How to override the standard row selection behaviour of a TDBGrid (Views: 701)
Problem/Question/Abstract: How to override the standard row selection behaviour of a TDBGrid Answer: The standard behaviour is as follows: Keyboard - If you hold down the Shift key and use the arrow keys the rows are continuously selected as you step through the grid. Clear all selections with the Escape key. Mouse - If you hold down the Ctrl key and click with the mouse you can select/ deselect individual rows. The following code allows selection/ deselection by hitting the spacebar and also allows stepping up/ down with the arrow keys without causing deselection. procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if (Shift = []) then begin case Key of VK_UP: begin Key := 0; TDBGrid(Sender).DataSource.DataSet.Prior; end; VK_Down: begin Key := 0; TDBGrid(Sender).DataSource.DataSet.Next; end; VK_Space: begin Key := 0; TDBGrid(Sender).SelectedRows.CurrentRowSelected := not (TDBGrid(Sender).SelectedRows.CurrentRowSelected); end; end; end; end; |