請問各位大大,如何DBGrid中加入DBCheck。 |
答題得分者是:Fishman
|
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
請問各位大大。 尋找討論區內許多的資料,嘗試去套用那些程序。
由其他大大,所提供的資料。卻都一直出現錯誤訊息。
以下,為我所參考的網頁。
http://www.delphi.club.tw/viewtopic.php?t=288&highlight=dbgrid+checkbox 以下,為我所修改後資料,麻煩大家提供我那裡是否有定義錯誤。
紅字部份為錯誤的地方。
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, DB, ADODB, Grids, DBGrids; type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBCheckBox1: TDBCheckBox;
procedure DBGrid1ColEnter(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure FormActivate(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
if DBGrid1.Columns[DBGrid1.SelectedIndex].Field = Table1Senior then
DBCheckBox1.Visible := True
else
DBCheckBox1.Visible := False;
end; procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if DBGrird1.Columns[DBGrird1.SelectedIndex].Field = Table1Senior then
DBCheckBox1.Visible := True
else
DBCheckBox1.Visible := False;
end; procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) and (Column.Field = Table1Senior) then
begin
DBCheckBox1.SetBounds(Rect.Left DBGrid1.Left 1, Rect.Top DBGrid1.Top 1,
Rect.Right - Rect.Left, Rect.Bottom - Rect.Top);
end; end; procedure TForm1.FormActivate(Sender: TObject);
begin
ADOQuery1.DataSource := ADOConnection1;
ADOQuery1.Close;
ADOQuery1.SQL.Add(' SELECT MC001,MC002,MC003,MC004,MC006 FROM BCSMC ');
ADOQuery1.Open;
ADOQuery1.Active := True;
DataSource1.DataSet := ADOQuery1;
DBGrid1.DataSource := DataSource1;
DBCheckBox1.Caption := 'MC006';
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.ValueChecked := True;
DBCheckBox1.ValueUnchecked := False;
DBCheckBox1.Visible := False; end; end. 謝謝。
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Table1Senior??? A persistent field? It is undefined in your code. Try compare the fieldname then, e.g. if DBGrid1.Columns[DBGrid1.SelectedIndex].Field.FieldName='MC001' then
..... http://pywong.hk.st http://www.lazybones.ca
|
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
謝謝您。 procedure TForm1.FormActivate(Sender: TObject);
begin
ADOQuery1.Connection := ADOConnection1;
ADOQuery1.Close;
ADOQuery1.SQL.Add(' SELECT MC001,MC002,MC003,MC004,MC006 FROM BCSMC ');
ADOQuery1.Open;
ADOQuery1.Active := True;
DataSource1.DataSet := ADOQuery1;
DBGrid1.DataSource := DataSource1;
DBCheckBox1.Caption := '';
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.ValueChecked := 'Y';
DBCheckBox1.ValueUnchecked := 'N';
DBCheckBox1.Visible := False; end; 我有分別去設定它們值,也沒有反應。
奇怪。
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
DBCheckBox1.DataField? http://pywong.hk.st
|
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
謝謝您。
因為有另外寫個小程式,都正常。
可是套用到我目前的使用的程式時。
發生DBCheckBox沒有乖乖的放在DBGrid內。
如下圖。
請問是否要調整,以下的程序呢。
if (gdFocused in State) then
begin
if (DataCol=7) then
begin
DBCheckBox2.Left :=Rect.Left + DBGrid1.Left+1;
DBCheckBox2.Top :=Rect.Top + DBGrid1.top+1;
DBCheckBox2.Width :=Rect.Right - Rect.Left;
DBCheckBox2.Height :=Rect.Bottom- Rect.Top;
DBCheckBox2.Visible:=True;
end;
end
else
if (DataCol=7) then
if Column.Field.Text='Y'
then DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageTrue.Picture.Bitmap)
else DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageFalse.Picture.Bitmap)
麻煩大家。^^
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
You need to convert the coordinates...
var Pt: TPoint; ... Pt := DBGrid1.ClientToScreen(Rect.TopLeft); Pt := Form1.ScreenToClient(Pt);Use Pt as the top left point for your DBCheckBox. http://pywong.hk.st |
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Before you draw the bitmap, clear the background...
with DBGrid1.Canvas do begin Brush.Style := bsSolid; Brush.Color := clWindow; FillRect(Rect); end;http://pywong.hk.st |
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
謝謝您。
with DBGrid1.Canvas do begin
Brush.Style := bsSolid;
Brush.Color := clWindow;
FillRect(Rect);
end;
如放在DBGrid1DrawColumnCell似乎一樣秀出值。
Check的圖示的下面還是有此欄位值,如Y或N。
另外,在此DBGrid點選該check如保稅品。
不是出現DBCheck可讓使用者點選修改呢。
但是當指標移動此列時,DBCheck依然停留於之前所點的那列。
是否應該要DBCheck應該不顯示呢。
如下圖。
麻煩您。
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
1. Fill the background before any custom drawing. Can you post the code?
2. Hide the DBCheckBox in DataSource1.OnDataChange for a cursor move, i.e. the parameter Field is nil inside the handler. http://pywong.hk.st
|
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
1.
if (DataCol=7) then begin DBGrid1.Canvas.FillRect(Rect); if Column.Field.Text='Y' then DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageTrue.Picture.Bitmap) else DBGrid1.Canvas.Draw(Rect.Left,Rect.Top,ImageFalse.Picture.Bitmap); end;2. example: procedure TFCBCSBOM.DataSource1DataChange(Sender: TObject; Field: TField); begin if not Assigned(Field) then DBCheckBox1.Hide; end;http://pywong.hk.st |
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Sorry!!插一下花!! 請參考以下連結:
http://delphi.ktop.com.tw/topic.php?topic_id=36136 範例如下:
http://delphi.ktop.com.tw/loadfile.php?TOPICID=24745167&CC=553413 ----------------------------------
小弟才疏學淺,若有謬誤尚請不吝指教
----------------------------------
------
Fishman |
t0288542
中階會員 發表:216 回覆:254 積分:94 註冊:2004-10-06 發送簡訊給我 |
|
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi stevenyehs, FillRect method (TCanvas) Fills the specified rectangle on the canvas using the current brush. Description Use FillRect to fill a rectangular region using the current brush. The region is filled including the top and left sides of the rectangle, but excluding the bottom and right edges. Canvas, Brush, Bitmap, FillRect example
var Bitmap: TBitmap; begin Bitmap := TBitmap.Create; try Bitmap.LoadFromFile('MyBitmap.bmppng'); Form1.Canvas.Brush.Bitmap := Bitmap; Form1.Canvas.FillRect(Rect(0,0,100,100)); finally Form1.Canvas.Brush.Bitmap := nil; Bitmap.Free; end; end;---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |