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

sra*_*ner 6 delphi tdbgrid delphi-2010

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

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

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

Ken*_*ite 15

您可以使用鼠标坐标来获取列.调用后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;
Run Code Online (Sandbox Code Playgroud)

有关文档TGridCoord中的更多信息.

使用与我之前的问题的答案相同的应用程序进行测试.