TDBGrid
我想通过代码激活一个单元格。“激活”是指用户在单元格内单击,准备编辑单元格内容。我怎么能这样做?
编辑:这可能涉及两个步骤:更改当前活动的单元格,然后进入编辑模式。
我的实现基于Andriy 出色的侦探工作:
type
TDBGridAccess = class(TDBGrid);
// Set the currently active grid cell to (DestCol, DestRow). Both values are
// relative to the currently _visible_ upper left grid cell.
procedure SelectDBGridCell(Grid: TDBGrid; DestCol, DestRow: Integer);
var
CurrentRow: Integer;
begin
if not Assigned(Grid.DataSource) or not Assigned(Grid.DataSource.DataSet) then
Exit;
CurrentRow := TDBGridAccess(Grid).Row;
Grid.DataSource.DataSet.MoveBy(DestRow-CurrentRow);
// check if the leftmost grid column is the indicator column which has no
// equivalent field in the dataset
if dgIndicator in Grid.Options then
Grid.SelectedIndex := DestCol-1 else
Grid.SelectedIndex := DestCol;
end;
procedure TDBGridController.HandleDBGridMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
CellAtMousePos: TGridCoord;
CurrentRow: Integer;
DBGrid: TDBGrid;
begin
DBGrid := Sender as TDBGrid;
CellAtMousePos := DBGrid.MouseCoord(X, Y);
if (CellAtMousePos.X<>-1) and (CellAtMousePos.Y<>-1) then
SelectDBGridCell(DBGrid, CellAtMousePos.X, CellAtMousePos.Y);
end;
(网格选择跟随鼠标光标。但SelectDBGridCell
也可用于根据其他条件选择单元格。)
从技术角度来看,它就像一个魅力。可用性是另一个问题。