全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2471
推到 Plurk!
推到 Facebook!

請問各位大大,如何DBGrid中加入DBCheck。

答題得分者是:Fishman
t0288542
中階會員


發表:216
回覆:254
積分:94
註冊:2004-10-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-27 10:31:40 IP:61.221.xxx.xxx 未訂閱
請問各位大大。    尋找討論區內許多的資料,嘗試去套用那些程序。 由其他大大,所提供的資料。卻都一直出現錯誤訊息。 以下,為我所參考的網頁。 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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-27 10:51:49 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-27 11:26:52 IP:61.221.xxx.xxx 未訂閱
謝謝您。終於有人回我。 現在沒有錯誤訊息。 可是DBCheckBox卻無法點選。 我嘗試加入 procedure TForm1.DBCheckBox1Click(Sender: TObject); begin SendMessage(DBCheckBox1.Handle, BM_GetCheck, 0, 0); end; 謝謝您。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-09-27 11:43:29 IP:147.8.xxx.xxx 未訂閱
DBCheckBox1.ValueChecked := 'Y;T'; DBCheckBox1.ValueUnchecked := 'N'; 發表人 - william 於 2005/09/27 11:47:35
t0288542
中階會員


發表:216
回覆:254
積分:94
註冊:2004-10-06

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-09-27 11:55:12 IP:61.221.xxx.xxx 未訂閱
謝謝您。 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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-09-27 12:23:04 IP:147.8.xxx.xxx 未訂閱
DBCheckBox1.DataField?    http://pywong.hk.st
t0288542
中階會員


發表:216
回覆:254
積分:94
註冊:2004-10-06

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-09-27 13:38:19 IP:61.221.xxx.xxx 未訂閱
謝謝您。 因為有另外寫個小程式,都正常。 可是套用到我目前的使用的程式時。 發生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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-09-27 14:22:03 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-09-27 16:37:54 IP:61.221.xxx.xxx 未訂閱
不好意思。麻煩您。 位置的問題,已經好嘍。 可以在問一個問題呢。 就是圖示的旁邊的值。 是否可以讓它不秀不出來。 試幾次好像都不行。 不曉得問題在那裡。 謝謝。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-09-27 16:52:16 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-09-28 09:52:21 IP:61.221.xxx.xxx 未訂閱
謝謝您。 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

發送簡訊給我
#12 引用回覆 回覆 發表時間:2005-09-28 10:38:35 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#13 引用回覆 回覆 發表時間:2005-09-28 16:01:33 IP:61.221.xxx.xxx 未訂閱
謝謝您。 我已將程式放置求助區中。 麻煩您。我在利用別的方式。 謝謝。
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#14 引用回覆 回覆 發表時間:2005-09-28 17:00:51 IP:147.8.xxx.xxx 未訂閱
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

發送簡訊給我
#15 引用回覆 回覆 發表時間:2005-09-28 18:26:27 IP:210.65.xxx.xxx 未訂閱
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

發送簡訊給我
#16 引用回覆 回覆 發表時間:2005-09-29 10:20:58 IP:61.221.xxx.xxx 未訂閱
謝謝william和Fishman你們兩位的幫忙。 麻煩你們這些多次。 對於Delphi而言,在學習方面一直都是獨自奮鬥。 時常不曉得什麼命令需要加在那裡。解決問題的方 式,除了查書外,最重要的因為有這個討論區,讓 我可以查出一些答案和程式技巧,同時也有各位大 大的指導。有些問題才可以解決。 ps.再次請教。是什麼意思。指清除所在格子內資料。 DBGrid1.Canvas.FillRect(Rect); 謝謝。
Fishman
尊榮會員


發表:120
回覆:1949
積分:2163
註冊:2006-10-28

發送簡訊給我
#17 引用回覆 回覆 發表時間:2005-09-29 11:56:01 IP:210.65.xxx.xxx 未訂閱
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
系統時間:2024-07-01 5:50:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!