delphi - 将 tdbgrid 的部分复制到剪贴板?

标签 delphi clipboard

是否有任何方法可以轻松地将所选行从 Delphi 2007 中的 TDBGrid 复制到剪贴板?

最佳答案

此方法来 self 们的内部图书馆单元..

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;

要将所选项目的列表添加到剪贴板,请将 Clipbrd 添加到您的 use 子句中并调用该过程。

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

当然,您可以修改上述方法或创建一个新方法,直接将所选项目添加到剪贴板(例如,多个字段,采用特殊格式等)

关于delphi - 将 tdbgrid 的部分复制到剪贴板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/397413/

相关文章:

javascript - jQuery - 更改选定的文本

c# - 关闭程序后保留剪贴板信息

delphi - 您的程序需要多少内存? (FastMM与Borland MM)

python - 使用 Clipboard_append 时的大括号

delphi - JEDI 控件总是重新编译

delphi - 如何在Delphi 7中不调试运行?

javascript - 使用 React,如何绑定(bind)到用户的剪贴板粘贴事件和解析数据?

python - PyGTK 剪贴板 set_text 的效果仅在进程运行时持续存在

delphi - 我应该如何为 TTreeNode 实现 GetLastNode?

德尔福2010 RTTI : Explore Enumerations