TDBGrid
コードでセルをアクティブにしたい。「アクティブ化」とは、ユーザーがセル内をクリックして、セルの内容を編集できるようにすることを意味します。どうすればこれを行うことができますか?
編集: これにはおそらく 2 つの手順が必要です: 現在アクティブなセルを変更し、編集モードに入ります。
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
、他の条件に基づいてセルを選択するためにも使用できます。)
技術的な観点からは魅力のように機能します。使いやすさは別の問題です。