Ehlib的DBGridEh首列加序號(hào)
必須有首列(建一個(gè))
dbgrideh-options-dgzndicator設(shè)置為false
dbgrideh.columns[0]-visual-color可以區(qū)分其他列的顏色
procedure TDBViewFrm.DBGridEh1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
begin
if Column.Index = 0 then
if DBGridEh1.SumList.RecNo <> -1 then
DBGridEh1.Canvas.TextRect(Rect, Rect.Left + 3, Rect.Top + 2,
IntToStr(DBGridEh1.SumList.RecNo));
end;
************************************************
procedure TDBViewFrm.DBGridEh1TitleClick(Column: TColumnEh);
//對(duì)dbgrid點(diǎn)擊標(biāo)題欄排序
var
i: Integer;
begin
if ADOQuery1.Active = True then
begin
for i := 1 to DBGridEh1.Columns.Count do
begin
//恢復(fù)所有標(biāo)題字體為默認(rèn)
DBGridEh1.Columns[i - 1].Title.Font.Color := clWindowText;
DBGridEh1.Columns[i - 1].Title.Font.Style := [];
end;
if (Column.Index <> 0) and (Column.Index <> 9) then //去掉不需要排序或不能排序的列。
begin
if ADOQuery1.Sort <> (Column.FieldName + ‘ ASC‘) then //判斷原排序方式
begin
ADOQuery1.Sort:= Column.FieldName + ‘ ASC‘;
Column.Title.Font.Color := clRed;
//改變標(biāo)題行字體為紅色,表示當(dāng)前的排序方式為升序
Column.Title.Font.Style := [fsBold];
end
else
begin
ADOQuery1.Sort:= Column.FieldName + ‘ DESC‘;
Column.Title.Font.Color := clBlue;
//改變標(biāo)題行字體為紅色,表示當(dāng)前的排序方式為降序
Column.Title.Font.Style := [fsBold];
end;
end;
end;
end;
*********************************************************************
DBGridEh中分行分列、單元格的顏色設(shè)置
(1)分行不同顏色設(shè)置;在DBGridEh1DrawColumnCell中寫;
if ADOQuery1.RecNo mod 2=0 then
begin
DBGridEh1.Canvas.Font.Color := clRed;
DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else begin
DBGridEh1.Canvas.Font.Color := clGreen;
DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
(2)分行不同背景顏色設(shè)置;在DBGridEh1DrawColumnCell中寫;
if ADOQuery1.RecNo mod 2=0 then
begin
DBGridEh1.Canvas.Brush.Color := clRed;
DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end
else begin
DBGridEh1.Canvas.Brush.Color := clGreen;
DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
(3)符合條件的單元格顏色或者背景顏色設(shè)置;在DBGridEh1DrawColumnCell中寫;
if Column.FieldName=‘價(jià)格‘ then
begin
if ADOQuery1.FieldByName(‘價(jià)格‘).AsFloat<0 then
begin
DBGridEh1.Canvas.Font.Color := clRed;
DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
end;
(4)交叉設(shè)置列顏色
1、為DBGridEh的每列的Color屬性設(shè)置值。
2、將DBGridEh的RowColorFlag設(shè)為false;
注:DBGridEh1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
Rect:代表在畫布中cell的位置位置所在,也就是你要對(duì)哪個(gè)區(qū)域進(jìn)行重畫;
DataCol:代表columns數(shù)組中column的標(biāo)號(hào)
Column:描述cell的顯示屬性和對(duì)應(yīng)的字段屬性的tcolumn對(duì)象
State:描述cell是否有輸入焦點(diǎn)、是否被選中、是否處于鎖定模式(如同column header)
*******************************************************************
(5)設(shè)置格線顏色
procedure Tfm1.FormCreate(Sender: TObject);
begin
dbgrideh2.FixedColor:=clWhite;
dbgrideh2.GridLineColors.DarkColor:=clRed;
dbgrideh2.GridLineColors.BrightColor:=clRed;
dbgrideh2.Flat:=true;
dbgrideh2.Ctl3D:=false; //這樣就可以設(shè)置邊框顏色
dbgrideh2.Options:=dbgrideh2.Options+[dgRowSelect];
end;
*******************************************************************
(5)多行選擇復(fù)制
procedure Tfm1.btnCopyClick(Sender:TObject);
var
i:integer;
booklist:TBookmarklist;
bookstr:Tbookmarkstr;
begin
bookstr:=Table1.bookmark;
try
booklist:=dbgrideh2.SelectedRows;
for i:=0 to booklist.count-1 do
begin
Table1.bookmark:=booklist[i];
Memo1.lines.add(Table1.fieldbyname(‘字段1‘).asstring);
Memo1.lines.add(Table1.fieldbyname(‘字段2‘).asstring);
end;
finally
Table1:=bookstr;
end;
end;