dbgrid 多行显示_选择并突出显示DBGrid中的一行

dbgrid 多行显示

Have you ever seen a menu or table column or row highlight to a different color when your mouse hovers over it? That's what our goal is here: to have a row become highlighted when the mouse pointer is within range.

当鼠标悬停在菜单或表格的列或行上时,您看到过其他颜色吗? 这就是我们的目标:当鼠标指针在范围内时,使一行突出显示。

The TDBGrid Delphi component is one of the jewels of the VCL. Designed to enable a user to view and edit data in a tabular grid, the DBGrid provides various ways of customizing the way it represents its own data. For example, adding color to your database grids will enhance the appearance and differentiate the importance of certain rows or columns within the database.

TDBGrid Delphi组件是VCL的亮点之一。 DBGrid旨在使用户能够查看和编辑表格网格中的数据,它提供了多种自定义其表示自己数据的方式的方法。 例如, 将颜色添加到数据库网格将增强外观并区分数据库中某些行或列的重要性。

However, don't be fooled by over-simplistic tutorials on this topic. It might seem easy enough to just set the dgRowSelect property, but remember that when dgRowSelect is included in Options, the dgEditing flag is ignored, meaning that editing the data using the grid is disabled.

但是,不要被关于这个主题的过于简单的教程所欺骗。 这似乎很容易,只设置dgRowSelect属性,但请记住,当被列入选项 dgRowSelectdgEditing标志被忽略,这意味着使用网格编辑数据被禁用。

What you'll find below is an explanation on how to enable the OnMouseOver type of event for a DBGrid row, so that the mouse is recorded and located, making the record active so as to highlight the corresponding row in a DBGrid.

您将在下面找到有关如何为DBGrid行启用OnMouseOver 事件类型的说明,以便记录和定位鼠标,使记录处于活动状态,以便突出显示DBGrid中的相应行。

如何使用OnMouseOver和Delphi组件 ( How to Work With OnMouseOver and Delphi Components )

The first order of business is writing code for the OnMouseMove event in a TDBGrid component so that it can locate the DBGrid's row and column (cell) that the mouse is hovering over.

首先要在TDBGrid组件中为OnMouseMove事件编写代码 ,以便它可以定位鼠标悬停在DBGrid的行和列(单元格)。

If the mouse is over the grid (handled in the OnMouseMove event handler), you can use the MoveBy method of a DataSet component to set the current record to the one displayed "below" the mouse cursor.

如果鼠标悬停在网格上方(在OnMouseMove事件处理程序中处理),则可以使用DataSet组件的MoveBy方法将当前记录设置为鼠标光标“下方”显示的记录。


type THackDBGrid = class(TDBGrid);
...procedure TForm1.DBGrid1MouseMove
(Sender: TObject; Shift: TShiftState; X, Y: Integer);var
gc: TGridCoord;begin
gc:= DBGrid1.MouseCoord(x, y);if (gc.X > 0) AND (gc.Y > 0) thenbegin
DBGrid1.DataSource.DataSet.MoveBy
(gc.Y - THackDBGrid(DBGrid1).Row);end;end;

Similar code can be used to show which cell the mouse hovers over and to change the cursor when it's over the title bar.

类似的代码可用于显示鼠标悬停在哪个单元格上,以及当鼠标悬停在标题栏上时更改光标。

In order to correctly set the active record, you need to hack a DBGrid and get your hands on the protected Row property. The Row property of a TCustomDBGrid component holds the reference to the currently active row.

为了正确设置活动记录,您需要破解DBGrid并使用受保护的Row属性。 TCustomDBGrid组件的Row属性保留对当前活动行的引用。

Many Delphi components have useful properties and methods that are marked invisible, or protected, to a Delphi developer. Hopefully, to access such protected members of a component, a simple technique called the "protected hack" can be used.

许多Delphi组件具有有用的属性和方法,这些属性和方法被Delphi开发人员标记为不可见或受保护。 希望访问组件的此类受保护成员,可以使用一种称为“受保护的hack”的简单技术。

With the code above, when you move the mouse over the grid, the selected record is the one displayed in the grid "below" the mouse cursor. There's no need to click the grid to change the current record.

使用上面的代码,将鼠标移到网格上方时,所选记录就是在鼠标光标“下方”的网格中显示的记录。 无需单击网格即可更改当前记录。

Have the active row highlighted to enhance the user's experience:

高亮显示活动行以增强用户的体验:


procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject; const Rect: TRect; DataCol: Integer;
Column: TColumn; State: TGridDrawState);beginif (THackDBGrid(DBGrid1).DataLink.ActiveRecord + 1 =
THackDBGrid(DBGrid1).Row)or (gdFocused in State) or (gdSelected in State) thenbegin
DBGrid1.Canvas.Brush.Color := clSkyBlue;
DBGrid1.Canvas.Font.Style := DBGrid1.Canvas.Font.Style + [fsBold];
DBGrid1.Canvas.Font.Color := clRed;end;end;

The OnDrawColumnCell event is used to handle the need for a customized drawing for the data in the cells of the grid.

OnDrawColumnCell事件用于处理对网格单元格中的数据进行自定义绘图的需求。

You can use a little trick to differentiate the selected row from all the other rows. Consider that the Row property (integer) is equal to the ActiveRecord (+1) property of the DataLink object that the selected row is about to be painted.

您可以使用一些技巧来将所选行与所有其他行区分开。 请考虑Row属性( integer )等于所选行将要绘制的DataLink对象的ActiveRecord (+1)属性。

You'll probably want to disable this behavior (the MoveBy method in OnMouseMove event handler) when DataSet connected to a DBGrid is in Edit or Insert mode.​

你可能会想禁用此行为(MoveBy方法的OnMouseMove事件处理程序)时, 数据集连接到一个DBGrid处于编辑插入模式。

翻译自: https://www.thoughtco.com/select-and-highlight-row-in-dbgrid-4077236

dbgrid 多行显示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Delphi 7 的 DBGrid 控件的每行数据显示一个按钮,你可以使用 TDBGridButtonColumn 组件并设置其 ButtonStyle 属性为 bsGlyph。 以下是一个简单的示例: 1. 在 Delphi 7 的窗体设计器,将一个 TDBGrid 组件放置在你的窗体上。 2. 在窗体上放置一个 TDataSource 组件,并将其 DataSet 属性设置为你要在 DBGrid 显示的数据集3. 打开 DBGrid 的 Columns 属性编辑器。击右侧的“添加”按钮,选择 "TColumn" 类型。 4. 在 "TColumn Editor" 对话框选择 "TDBGridButtonColumn" 类型。 5. 在 "TDBGridButtonColumn Editor" 对话框,设置 ButtonStyle 属性为 bsGlyph。 6. 在 "TDBGridButtonColumn Editor" 对话框,为 Glyph 属性选择一个合适的图标,用于表示按钮。 7. 在 DBGrid 的 OnDrawColumnCell 事件编写代码来绘制按钮。 ```delphi procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var ButtonRect: TRect; ButtonWidth: Integer; begin if (Column is TDBGridButtonColumn) and (gdFocused in State) then begin ButtonWidth := 18; // 按钮的宽度 ButtonRect.Left := Rect.Right - ButtonWidth - 2; ButtonRect.Right := Rect.Right - 2; ButtonRect.Top := Rect.Top + 2; ButtonRect.Bottom := Rect.Bottom - 2; DBGrid1.Canvas.Pen.Color := clBtnFace; DBGrid1.Canvas.Brush.Color := clBtnFace; DBGrid1.Canvas.Rectangle(ButtonRect); // 在按钮上绘制图标 ImageList1.Draw(DBGrid1.Canvas, ButtonRect.Left + 2, ButtonRect.Top + 2, 0); end; end; ``` 请确保你已经将一个 TImageList 组件放置在窗体上,并将其 Images 属性设置为包含所需图标的图像列表。 现在,每行数据将在最后一列显示一个带有图标的按钮。你可以根据需要自定义按钮的样式、位置和绘制方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值