简体   繁体   中英

Copying portions of a tdbgrid to clipboard?

有什么方法可以轻松地将所选行从Delphi 2007中的TDBGrid复制到剪贴板吗?

This method is from our internal library unit..

procedure BuildListFromDBGrid(DBGrid: TDBGrid; const FieldName: String; Strings :TStrings);
var
  i: Integer;
begin
  Strings.Clear();
  with DBGrid do
     begin
        Strings.BeginUpdate(); // If assocated with a UI control (Listbox, etc), this will prevent any flickering
        DataSource.DataSet.DisableControls();
        try
          for i := 0 to (SelectedRows.Count - 1) do
             begin
               Datasource.DataSet.GotoBookmark(Pointer(SelectedRows[i]));
               Strings.Add(DataSource.DataSet.FieldByName(FieldName).AsString);
             end;
        finally
           DataSource.DataSet.EnableControls();
           Strings.EndUpdate();
        end;
     end;
end;

To get the list of selected items to the clipboard, add Clipbrd to your uses clause and call the procedure..

var
  SelectedItems :TStringList;
begin
  SelectedItems := TStringList.Create();
  try
    BuildListFromDBGrid(MyDBGrid, 'InvoiceID', SelectedItems);
    Clipboard.AsText := SelectedItems.Text;
  finally
    SelectedItems.Free();
  end;
end;

Of course you could modify the above method or create a new one that directly adds the selected items to the clipboard (ex, multiple fields, in a specialized format, etc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM