2004. december 4., szombat

How to transfer data between a TDBGrid and the clipboard


Problem/Question/Abstract:

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

procedure TMyForm.MyDBGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
{OnKeyDown event handler for your DBGrid}
const
  vk_c = $43;
  vk_v = $56;
begin
  if Shift = [ssCtrl] then
  begin
    if key = vk_v then
      Shift := [ssShift];
    if (key = vk_c) or (key = vk_v) then
    begin
      gk := Key;
      key := 0;
    end;
  end;
end;

procedure TMyForm.MyDBGridKeyPress(Sender: TObject; var Key: Char);
{OnKeyPress event handler for your DBGrid}
const
  vk_c = $43;
  vk_v = $56;
begin
  if gk <> 0 then
  begin
    Key := chr(0);
    if gk = vk_c then
      ClipBoard.AsText := MyTable.Fields[MyDBGrid.SelectedIndex].AsString;
    if gk = vk_v then
    begin
      if (MyTable.State = dsEdit) or (MyTable.State = dsInsert) then
        MyTable.Fields[MyDBGrid.SelectedIndex].AsString := ClipBoard.AsText
      else
        MessageBeep(0);
    end;
    gk := 0;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése