tdbgrid网格线颜色_如何在TDBGrid组件中更改颜色

tdbgrid网格线颜色

Adding color to your database grids will enhance the appearance and differentiate the importance of certain rows or columns within the database. We'll do this by focusing on DBGrid, which provides a great user interface tool for displaying data.

数据库网格添加颜色将增强外观,并区分数据库中某些行或列的重要性。 我们将重点关注DBGrid ,它提供了一个出色的用于显示数据的用户界面工具。

We'll assume that you already know how to connect a database to a DBGrid component. The easiest way to accomplish this is to use the Database Form Wizard. Select the employee.db from the DBDemos alias and select all fields except EmpNo.

我们假设您已经知道如何将数据库连接到DBGrid组件。 完成此操作的最简单方法是使用数据库表单向导。 从DBDemos别名中选择employee.db ,然后选择除EmpNo之外的所有字段。

着色栏 ( Coloring Columns )

The first and easiest thing you can do to visually enhance the user interface is to color individual columns in the data-aware grid. We'll accomplish this through the TColumns property of the grid.

要视觉上增强用户界面 ,首先要做的最简单的事情就是为数据感知网格中的各个列着色。 我们将通过网格的TColumns属性完成此操作。

Select the grid component in the form and invoke the Columns editor by double-clicking the grid's Columns property in the Object Inspector.

在窗体中选择网格组件,然后通过在对象检查器中双击网格的Columns属性来调用“列”编辑器。

The only thing left to do is specify the background color of the cells for any particular column. For text foreground color, see the font property.

剩下要做的就是为任何特定的列指定单元格的背景颜色。 有关文本前景色,请参见font属性。

Tip: For more information on Columns editor, look for Columns editor: creating persistent columns in your Delphi help files.

提示:有关Columns编辑器的更多信息,请查找Columns编辑器:Delphi帮助文件中创建永久列

着色行 ( Coloring Rows )

If you want to color the selected row in a DBGrid but you don't want to use the dgRowSelect option (because you want to be able to edit the data), you should instead use the DBGrid.OnDrawColumnCell event.

如果要为DBGrid中的选定行着色,但又不想使用dgRowSelect选项(因为您希望能够编辑数据),则应该使用DBGrid.OnDrawColumnCell事件。

This technique demonstrates how to dynamically change the color of text in a DBGrid:

这种技术演示了如何动态地改变文本的颜色在一个DBGrid:


procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn;
State: TGridDrawState);beginif Table1.FieldByName('Salary').AsCurrency>36000 then
DBGrid1.Canvas.Font.Color:=clMaroon;
DBGrid1.DefaultDrawColumnCell
(Rect, DataCol, Column, State);end;

Here's how to dynamically change the color of a row in a DBGrid:

这是动态更改颜色的方法 在DBGrid中:


procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn;
State: TGridDrawState);beginif Table1.FieldByName('Salary').AsCurrency>36000 then
DBGrid1.Canvas.Brush.Color:=clWhite;
DBGrid1.DefaultDrawColumnCell
(Rect, DataCol, Column, State);end;

着色细胞 ( Coloring Cells )

Finally, here's how to change the background color of the cells of any particular column, plus the text foreground color:

最后,以下是更改任何特定列的单元格的背景颜色以及文本前景色的方法


procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn;
State: TGridDrawState);beginif Table1.FieldByName('Salary').AsCurrency>40000 thenbegin
DBGrid1.Canvas.Font.Color:=clWhite;
DBGrid1.Canvas.Brush.Color:=clBlack;end;if DataCol = 4 then //4 th column is 'Salary'
DBGrid1.DefaultDrawColumnCell
(Rect, DataCol, Column, State);end;

As you can see, if an employee's salary is greater than 40 thousand, its Salary cell is displayed in black and the text is displayed in white.

如您所见,如果雇员的薪水大于4万,则其薪金单元格将显示为黑色,文本将显示为白色。

翻译自: https://www.thoughtco.com/change-coloring-in-tdbgrid-component-4077252

tdbgrid网格线颜色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值