DatagridView : Cannot delete rows

0

I want to clear data grid view without delete column settings. One column is a DataGridViewComboBoxColumn.

I use the following code :

gridNomiColonne.Rows.Clear();
gridNomiColonne.Columns.Clear();

I get the following error:

"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 )

Any ideas?

c#
winforms
gridview
asked on Stack Overflow Apr 22, 2018 by Simone Spagna • edited Apr 22, 2018 by user6910411

1 Answer

0

You can use something like:

gridNomiColonne.DataSource = (gridNomiColonne.DataSource as DataTable).Clone();

this will delete all the rows in the grid but will keep the structure of the table.

if you would like to clear all the data, you can use

gridNomiColonne.DataSource= null;
answered on Stack Overflow Apr 22, 2018 by Dor Lugasi

User contributions licensed under CC BY-SA 3.0