在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。 procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState); var i :integer; begin if gdSelected in State then Exit; //定义表头的字体和背景颜色: for i :=0 to (Sender as TDBGrid).Columns.Count-1 do begin (Sender as TDBGrid).Columns[i].Title.Font.Name :=\'宋体\'; //字体 (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //字体大小 (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //字体颜色(红色) (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //背景色(绿色) end; //隔行改变网格背景色: if Query1.RecNo mod 2 = 0 then (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //定义背景颜色 else (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色 //定义网格线的颜色: DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); with (Sender as TDBGrid).Canvas do //画 cell 的边框 begin Pen.Color := $00ff0000; //定义画笔颜色(蓝色) MoveTo(Rect.Left, Rect.Bottom); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线 Pen.Color := $0000ff00; //定义画笔颜色(绿色) MoveTo(Rect.Right, Rect.Top); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画绿色的竖线 end; end;
Delphi7 - 隔行改变DBGrid网格颜色 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var i:integer; begin if gdSelected in State then Exit; //隔行改变网格背景色: if adoQuery1.RecNo mod 2 = 0 then (Sender as TDBGrid).Canvas.Brush.Color := clinfobk //定义背景颜色 else (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //定义背景颜色 //定义网格线的颜色: DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); with (Sender as TDBGrid).Canvas do //画 cell 的边框 begin Pen.Color := $00ff0000; //定义画笔颜色(蓝色) MoveTo(Rect.Left, Rect.Bottom); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画蓝色的横线 Pen.Color := clbtnface; //定义画笔颜色(兰色) MoveTo(Rect.Right, Rect.Top); //画笔定位 LineTo(Rect.Right, Rect.Bottom); //画绿色 end; end;
Delphi5用BDE中的table1未能通过,颜色没有隔行变化。
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); begin if (gdFocused in State) then begin if (Field.FieldName = DBComboBox1.DataField ) then begin DBComboBox1.Left := Rect.Left + DBGrid1.Left; DBComboBox1.Top := Rect.Top + DBGrid1.top; DBComboBox1.Width := Rect.Right - Rect.Left; DBComboBox1.Height := Rect.Bottom - Rect.Top; DBComboBox1.Visible := True; end; end; end;
5、DBGrid指定单元格未获得焦点时不显示DBComboBox,设置DBGrid1的OnColExit事件如下: procedure TForm1.DBGrid1ColExit(Sender: TObject); begin If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then begin DBComboBox1.Visible := false; end; end;
6、当DBGrid指定列获得焦点时DrawDataCell事件只是绘制单元格,并显示DBComboBox,但是DBComboBox并没有获得焦点,数据的输入还是在单元格上进行。在DBGrid1的KeyPress事件中调用SendMessage这个 Windows API函数将数据输入传输到DBComboBox上,从而达到在DBComboBox上进行数据输入。因此还要设置KeyPress事件如下: procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char); begin if (key < > chr(9)) then begin if (DBGrid1.SelectedField.FieldName =DBComboBox1.DataField) then begin DBComboBox1.SetFocus; SendMessage(DBComboBox1.Handle,WM_Char,word(Key),0); end; end; end;
程序在中文Windows 98,Delphi 4.015 下调试通过。希望本文能使你可以更加方便快捷的开发数据库应用程序。 OnDrawDataCell(...) begin with TDBGrid(Sender) do begin if (条件) then Canvas.TextOut(Rect.Left + 4 Rect.Top + 2 \'要显示的文字如表格的资料\'); end;
而你会看到 DBGrid 的显示资料怎麽有重叠的情况那是因为原本DBGrid要显示的资料与 TextOut 所显示的资料重叠 procedure TForm1.Query1Detail1GetText(Sender: TField; var Text: string; DisplayText: Boolean); begin // 决定在 DBGrid 得知表格资料时要不要显示所得到的资料False -> 不显示 // 就可避免与 TextOut 的文字重叠了 DisplayText : = False; end; end;
如果用 Delphi 3 处理很简单.例如:对表中某字段当其数值小于0时为红字其他为黑字. begin if TableField.AsInteger < 0 then DBGrid.Canvas.Font.Color := clRed else DBGrid.Canvas.Font.Color := clBlack; DBGrid.DefaultDrawColumnCell(...); end;
这样对 Field 指定的格式仍旧生效不必重写. Case DataCol Mod 2 = 0 of True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色 False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色 End; DbGrid1.Canvas.Pen.Mode:=pmMask; DbGrid1.DefaultDrawColumnCell (RectDataCol Column State);
2. 纵向斑马线,同时以红色突出显示当前单元格效果:以突出显示当前选中的字段。 Case DataCol Mod 2 = 0 of True: DbGrid1.Canvas.Brush.Color:= clBlue; file://偶数列用蓝色 False: DbGrid1.Canvas.Brush.Color:= clAqua; file://奇数列用浅绿色 End;
上述两种方法突出了列的显示效果。 if ((State = [gdSelected]) or (State=[gdSelected gdFocused])) then DbGrid1.Canvas.Brush.color:=clRed; file://当前行以红色显示,其它行使用背景的浅绿色 DbGrid1.Canvas.pen.mode:=pmmask; DbGrid1.DefaultDrawColumnCell (RectDataColColumnState);
4.行突显的斑马线效果:既突出当前行,又区分不同的列(字段)。 if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then begin Case DataCol Mod 2 = 0 of True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列显示红色 False: DbGrid1.Canvas.Brush.color:=clblue; file://当前选中行的奇数列显示蓝色 end; DbGrid1.Canvas.pen.mode:=pmmask; DbGrid1.DefaultDrawColumnCell (RectDataColColumnState); end;
5.横向斑马线, 同时以红色突显当前行效果。 Case Table1.RecNo mod 2 = 0 of file://根据数据集的记录号进行判断 True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示 False: DbGrid1.Canvas.Brush.color:=clblue; file://奇数行用蓝色表示 end; if ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then file://选中行用红色显示 DbGrid1.Canvas.Brush.color:=clRed; DbGrid1.Canvas.pen.mode:=pmMask; DbGrid1.DefaultDrawColumnCell (RectDataColColumnState);
6.双向斑马线效果:即行间用不同色区分,同时,选中行以纵向斑马线效果区分不同的列。 Case Table1.RecNo mod 2 = 0 of file://根据数据集的记录号进行判断 True : DbGrid1.Canvas.Brush.color:=clAqua; file://偶数行用浅绿色显示 False: DbGrid1.Canvas.Brush.color:= clblue; file://奇数行用蓝色表示 end; If ((State = [gdSelected]) or (State=[gdSelectedgdFocused])) then Case DataCol mod 2 = 0 of True : DbGrid1.Canvas.Brush.color:=clRed; file://当前选中行的偶数列用红色 False: DbGrid1.Canvas.Brush.color:= clGreen; file://当前选中行的奇数列用绿色表示 end; DbGrid1.Canvas.pen.mode:=pmMask; DbGrid1.DefaultDrawColumnCell (RectDataColColumnState);
上述6种方法分别就数据网格控件的列和行的色彩进行了设置,读者可以根据自己的需要设置特效。该程序在Delphi5中测试通过。 procedure TFHkdata.SortQuery(Column:TColumn); var SqlStr,myFieldName,TempStr: string; OrderPos: integer; SavedParams: TParams; begin if not (Column.Field.FieldKind in [fkData,fkLookup]) then exit; if Column.Field.FieldKind =fkData then 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论