如何在TDBGrid中设置活动单元格? [英] How to set active cell in TDBGrid?

查看:109
本文介绍了如何在TDBGrid中设置活动单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过代码激活 TDBGrid 中的单元格。 激活是指就像用户在单元格中单击一样,准备编辑单元格内容。我该怎么做?

I want to activate a cell in a TDBGrid by code. By "activate" I mean like the user clicked inside the cell, ready to edit the cell content. How could I do this?

编辑:这可能涉及两个步骤:更改当前活动的单元格,然后进入编辑模式。

This probably involves two steps: change the currently active cell, then enter edit mode.

推荐答案

如果您是说激活当前活动单元格的编辑模式 ,那么您可能应该这样做:

If you mean ‘activate the edit mode for the currently active cell’, then you should probably do like this:

MyDBGrid.EditorMode := True;

激活特定单元格可以通过 SelectedIndex

Activating a particular cell can be done either via SelectedIndex:

MyDBGrid.SelectedIndex := 2;  { or maybe MyDBGrid.SelectedIndex + 1 }

或通过 SelectedField

MyDBGrid.SelectedField := MyDataSet.FieldByName('Name');

要确定当前哪个单元格位于鼠标光标下,可以使用 MouseCoord ,它返回一个 TGridCoord 记录,其中包含光标下单元格的坐标。 TGridCoord.X 字段可直接用于设置网格的活动列。

To determine which cell is under the mouse cursor at the moment, you can use MouseCoord, which returns a TGridCoord record holding the coordinates of the cell under the cursor. The TGridCoord.X field can be used directly to set the grid's active column.

var
  Cell: TGridCoord;

...

Cell := MyDBGrid.MouseCoord(X, Y);
MyDBGrid.SelectedIndex := Cell.X;

设置行比较棘手,到目前为止,我唯一能找到的方法就是所谓的 protected hack ,即访问受保护属性的方法和类的方法。这是我们需要破解的 TDBGrid 类。

Setting the row is trickier, and so far the only way I could find involves the so called protected hack, the method of accessing protected properties and methods of a class. And it's the TDBGrid class that we need to ‘hack’.

基本上,您声明 TDBGrid ,像这样:

Basically, you declare an empty descendant of TDBGrid, like this:

type
  THackDBGrid = class(TDBGrid);

然后,当您需要访问受保护的属性或方法时,只需将标准实例转换为类(在这种情况下为 MyDBGrid )为 hacked类型( THackDBGrid ):

Then, when you need to access a protected property or method, you simply cast the instance of a standard class (MyDBGrid in this case) to the ‘hacked’ type (THackDBGrid):

… THackDBGrid(MyDBGrid).protected_property_or_method

我们感兴趣的项目是 Row 属性。它返回活动行的Y坐标。我们需要知道它来确定活动行和光标下的行之间的差异,因此我们可以相应地移动基础数据集的记录指针。方法如下:

The item we are interested in is the Row property. It returns the Y coordinate of the active row. We need to know it to determine the difference between the active row and the one under the cursor, so we could then move the underlying dataset's record pointer accordingly. Here's how:

MyDataSet.MoveBy(Cell.Y - THackDBGrid(MyDBGrid).Row);

Row 的值不是绝对的:它相对于可见的顶部行,但是 TGridCoord.Y 也是,因此两者之间的差异对应于基础数据集中数据行之间的差异。

The Row value is not absolute: it is relative to the visible top row, but so is TGridCoord.Y, so the difference between the two corresponds to the difference between the data rows in the underlying dataset.

我想强调的一件事:应谨慎使用这种受保护的hack方法。受保护的物品受到保护是有原因的。因此,如果可以避免,请这样做。而且,如果您做不到(没有其他方法,或者它可以帮助您更轻松地完成工作),请记住避免直接使用受保护的骇客更改任何内容。我的意思是,可能没事,但是通常您永远无法确定。可以看到,我只使用了 read 方法来读取受保护的内容,没有直接更改任何内容。对象的状态最终发生了变化,但这是由 MoveBy 方法触发的标准机制的结果。

One thing that I'd like to stress: this protected hack method should be used discreetly. Protected items are protected for a reason. So, if you can avoid it, please do so. And if you can't (there's no other way or it helps you to do things much more easily), please remember to refrain from changing anything directly using protected hack. I mean, it might be all right, but generally you never know for sure. You can see that I only used the method to read the protected contents, I didn't change anything directly. The object's state was eventually changed, but that was the result of a standard mechanism triggered by the MoveBy method.

您可以阅读有关受保护的黑客的更多信息此处

You can read more about protected hack here.

这篇关于如何在TDBGrid中设置活动单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆