Author: Jonas Bilinkevicius
How to transfer data between a TDBGrid and the clipboard
Answer:
The grid must be in Edit or Insert mode for the paste to work.
Add 'ClipBrd' to the Uses list
Add 'gk: Word;' to your global variables
Add the following procedures to Implementation, substituting names as required
1 2 procedure TMyForm.MyDBGridKeyDown(Sender: TObject; var Key: Word; Shift:
3 TShiftState);
4 {OnKeyDown event handler for your DBGrid}5 const6 vk_c = $43;
7 vk_v = $56;
8 begin9 if Shift = [ssCtrl] then10 begin11 if key = vk_v then12 Shift := [ssShift];
13 if (key = vk_c) or (key = vk_v) then14 begin15 gk := Key;
16 key := 0;
17 end;
18 end;
19 end;
20 21 procedure TMyForm.MyDBGridKeyPress(Sender: TObject; var Key: Char);
22 {OnKeyPress event handler for your DBGrid}23 const24 vk_c = $43;
25 vk_v = $56;
26 begin27 if gk <> 0 then28 begin29 Key := chr(0);
30 if gk = vk_c then31 ClipBoard.AsText := MyTable.Fields[MyDBGrid.SelectedIndex].AsString;
32 if gk = vk_v then33 begin34 if (MyTable.State = dsEdit) or (MyTable.State = dsInsert) then35 MyTable.Fields[MyDBGrid.SelectedIndex].AsString := ClipBoard.AsText
36 else37 MessageBeep(0);
38 end;
39 gk := 0;
40 end;
41 end;