当dgRowSelect设置为True时,检测在TDBGrid中单击了哪个单元格?

时间:2012-12-18 16:57:03

标签: delphi delphi-2010 tdbgrid

在Delphi 2010中,有什么方法可以检测dgRowSelect设置为True时单击了哪个单元格?

通常我会使用OnCellClick(Column: TColumn)事件处理程序,但这不能按预期工作。使用dgRowSelect = False,此过程将传递已单击的列,但使用dgRowSelect = True此过程将传递到第一列,无论单击哪个列。

我无法解决调用OnCellClick传递TColumn参数的代码的位置,如果我发现我可能能够解决如何修复这种奇怪的行为。

1 个答案:

答案 0 :(得分:14)

您可以使用鼠标坐标来获取列。调用TDBGrid.MouseCoord后,返回的TGridCoord.X包含列号,Y包含行(当然,您已经拥有):

procedure TForm1.DBGrid1CellClick(Column: TColumn);
var
  Pt: TPoint;
  Coord: TGridCoord;
  ClickCol: Integer;
begin
  Pt := DBGrid1.ScreenToClient(Mouse.CursorPos);
  Coord := DBGrid1.MouseCoord(Pt.X, Pt.Y);
  ClickCol := Coord.X;
  ShowMessage('You clicked column ' + IntToStr(ClickCol));
end;

documentationTGridCoord的更多信息。

使用与your previous question的答案相同的应用程序进行测试。