我想清除数据网格视图而不删除列设置。
一列是DataGridViewComboBoxColumn
。
我使用以下代码:
gridNomiColonne.Rows.Clear();
gridNomiColonne.Columns.Clear();
我收到以下错误:
"Cannot delete data" (
System.ArgumentException
HResult=0x80070057
Messaggio=Impossibile cancellare questo elenco.
Origine=System.Data
Analisi dello stack:
in System.Data.DataView.System.Collections.IList.Clear()
in System.Windows.Forms.BindingSource.Clear()
in System.Windows.Forms.DataGridViewRowCollection.Clear()
in ContabImporterf.MainForm.btnCarica_Click(Object sender, EventArgs e) in
C:\Users\Utente\Documents\Visual Studio 2017\Projects\ContabImporterf
\ContabImporterf\MainForm.cs: riga 200
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button,
Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in
Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNai
iveMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32
reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
reason, ApplicationContext context)
in System.Windows.Forms.Application.Run(Form mainForm)
in ContabImporterf.Program.Main() in C:\Users\Utente\Documents\Visual Studio
2017\Projects\ContabImporterf\ContabImporterf\Program.cs: riga 19 )
有什么想法吗?
答案 0 :(得分:0)
您可以使用以下内容:
gridNomiColonne.DataSource = (gridNomiColonne.DataSource as DataTable).Clone();
这将删除网格中的所有行,但会保留表格的结构。
如果您想清除所有数据,可以使用
gridNomiColonne.DataSource= null;