簡體   English   中英

從datagridview復制帶有標題的單元格

[英]Copy cell with header from datagridview

我想從帶有列標題的datagridview復制一個單元格。 (不是行標題)。

我試過了,但出現異常:

if (dataGridView1.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
    foreach (DataGridViewColumn c in dataGridView1.Columns)
    {
        c.SortMode = DataGridViewColumnSortMode.NotSortable;
    }

    dataGridView1.SelectionMode = DataGridviewSelectionMode.ColumnHeaderSelect;                 //only the column headers will be copied
    dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithAutoHeaderText;   //.EnableWithAutoHeaderText;
                                                                                                                                // Add the selection to the clipboard.
    Clipboard.SetDataObject(dataGridView1.GetClipboardContent());  //Here it goes wrong
}

這是我得到的例外。 GetClipbooard的內容是否為空?

System.ArgumentNullException: 'Value cannot be null.
Parameter name: data'

System.ArgumentNullException occurred
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: data
  Source=System.Windows.Forms



StackTrace:
   at System.Windows.Forms.Clipboard.SetDataObject(Object data, Boolean copy, Int32 retryTimes, Int32 retryDelay)
   at System.Windows.Forms.Clipboard.SetDataObject(Object data)
   at Q_ProtoType.FormMain.MenuItemNew_Click(Object sender, EventArgs e) in c:\Data\Programma\Development\VSSource - oefen_new_functions\Q_ProtoType\Q_ProtoType\FormMain.cs:line 895
   at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.MenuItem.MenuItemData.Execute()
   at System.Windows.Forms.Command.Invoke()
   at System.Windows.Forms.Command.DispatchID(Int32 id)
   at System.Windows.Forms.Control.WmCommand(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Q_ProtoType.Program.Main() in c:\Data\Programma\Development\VSSource - oefen_new_functions\Q_ProtoType\Qx_ProtoType\Program.cs:line 42

您已設置ColumnHeaderSelectFullColumnSelect 因此,在調用GetClipboardContent您至少應選擇一列,否則您將收到一個空值作為剪貼板內容,而ClipBoard.SetDataObject將拋出ArgumentNullException

您可以使用dataGridView1.SelectAll(); 如果要導出所有列或例如要選擇第一列,則選擇所有列,可以設置dataGridview1.Columns[0].Selected = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM