[英]Copy cell with header from datagridview
I want to copy a cell from a datagridview with the columnheader. 我想从带有列标题的datagridview复制一个单元格。 (Not the row header). (不是行标题)。
I tried this, but i get an exception: 我试过了,但出现异常:
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
}
This is the Exception i get. 这是我得到的例外。 GetClipbooard content is empty ? 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
You have set ColumnHeaderSelect
to FullColumnSelect
. 您已设置ColumnHeaderSelect
到FullColumnSelect
。 So when calling GetClipboardContent
you should have at least one column selected, otherwise you will receive a null value as clipboard content and ClipBoard.SetDataObject
will throw an ArgumentNullException
. 因此,在调用GetClipboardContent
您至少应选择一列,否则您将收到一个空值作为剪贴板内容,而ClipBoard.SetDataObject
将抛出ArgumentNullException
。
You can use dataGridView1.SelectAll();
您可以使用dataGridView1.SelectAll();
to select all columns if you want to export them all or for example if you want to select the first column, you can set dataGridview1.Columns[0].Selected = true;
如果要导出所有列或例如要选择第一列,则选择所有列,可以设置dataGridview1.Columns[0].Selected = true;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.