首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从datagridview复制带有标题的单元格

从datagridview复制带有标题的单元格
EN

Stack Overflow用户
提问于 2017-12-11 00:00:27
回答 1查看 1.2K关注 0票数 0

我想从具有列标题的datagridview中复制一个单元格。(不是行标题)。

我试过了,但我得到了一个异常:

代码语言:javascript
复制
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内容为空?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-12-11 00:39:01

您已将ColumnHeaderSelect设置为FullColumnSelect。因此,在调用GetClipboardContent时,您应该至少选择一列,否则将收到一个NULL值作为剪贴板内容,并且ClipBoard.SetDataObject将抛出ArgumentNullException

如果要导出所有列,则可以使用dataGridView1.SelectAll();选择所有列;例如,如果要选择第一列,则可以设置dataGridview1.Columns[0].Selected = true;

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47740609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档