Dbgrid 變色問題 |
答題得分者是:hagar
|
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
當然用 RecNo 可能不準。
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var Canvas: TCanvas; begin Canvas := (Sender as TDBGrid).Canvas; if((Field.DataSet.RecNo mod 2)=0) then Canvas.Brush.Color := clRed else Canvas.Brush.Color := clBlue; Canvas.FillRect(Rect); DBGrid1.DefaultDrawDataCell(Rect, Field, State); end;-- 將進酒 杯莫停 與君歌一曲 請君為我傾耳聽 |
banson1716
高階會員 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
可更改 dbgrid 顏色的區顯示
var
Form1: TForm1;
Grid_integer1: Integer= 1 ;
Grid_integer: Integer= 2 ; implementation {$R *.DFM} procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
If gdFocused in State then
with (Sender as TDBGrid).Canvas do
begin
Brush.Color := clRed;
FillRect(Rect);
TextOut(Rect.Left, Rect.Top, Field.AsString);
end;
end; procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
ii : integer;
oldcolor:tcolor;
oldpm:tpenmode;
begin
if Table1.RecNo mod Grid_integer = Grid_integer1 then
begin
oldpm:= DBGrid1.Canvas.pen.mode;
oldcolor:= DBGrid1.Canvas.Brush.color;
DBGrid1.Canvas.Brush.color:= clYellow;//橫格色彩
DBGrid1.Canvas.Font.Color:= clRed;
DBGrid1.Canvas.pen.mode:= pmMergeNotPen;
DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
DBGrid1.Canvas.Brush.color:= oldcolor;
DBGrid1.Canvas.pen.mode:= oldpm;
end;
with DBGrid1.Canvas do
begin
Pen.Color := clRed;//橫線色彩
MoveTo(Rect.Left, Rect.Bottom);
LineTo(Rect.Right, Rect.Bottom);
Pen.Color := Clblue ;//縱線色彩
MoveTo(Rect.Right, Rect.Top);
LineTo(Rect.Right, Rect.Bottom);
end;
//DbGrid1.Canvas.DrawFocusRect(Rect);//格線加粗
end;
----------------------------
procedure TGridColorForm.SpeedButton1Click(Sender: TObject);
begin
try
if Edit1.Text = '0' then Grid_integer:= 1;
if Edit2.Text = '0' then Grid_integer1:= 2;
Finally
Grid_integer1:= StrToInt(Edit1.Text);
Grid_integer:= StrToInt(Edit2.Text);
NumBerForm.UNN1Click(Sender);
Close;
end;
end;
|
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
引言: 當然用 RecNo 可能不準。如果我在dbgrid上columns editing,以上的程式碼就無效,請問要如何修改??procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var Canvas: TCanvas; begin Canvas := (Sender as TDBGrid).Canvas; if((Field.DataSet.RecNo mod 2)=0) then Canvas.Brush.Color := clRed else Canvas.Brush.Color := clBlue; Canvas.FillRect(Rect); DBGrid1.DefaultDrawDataCell(Rect, Field, State); end;-- 將進酒 杯莫停 與君歌一曲 請君為我傾耳聽 |
cyl
中階會員 發表:163 回覆:171 積分:66 註冊:2002-07-11 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
原來是在 TDBGrid 中加入了 Column
將原程式碼做些修改, 放在 OnDrawColumnCell 事件即可。
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var Canvas: TCanvas; begin Canvas := (Sender as TDBGrid).Canvas; if((Column.Field.DataSet.RecNo mod 2)=0) then Canvas.Brush.Color := clRed else Canvas.Brush.Color := clBlue; Canvas.FillRect(Rect); DBGrid1.DefaultDrawDataCell(Rect, Column.Field, State); end;-- 將進酒 杯莫停 與君歌一曲 請君為我傾耳聽 |
sfy257000
一般會員 發表:21 回覆:13 積分:6 註冊:2002-07-11 發送簡訊給我 |
|
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
看一下 help 的說明吧:
Description As implemented in TDataSet, the value of RecNo is always -1.
Ordinarily an application does not access RecNo at the TDataSet
level. Instead a redeclared and implemented RecNo property in a
descendant class such as TTable is accessed. RecNo provides a
fallback property for derived datasets that do not support record
numbers. ---
歡迎光臨 KTop 研究院
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |