I have a dataGridView and an arrayList.
I bind the arrayList to my DataGridView with:
dataGridView.DataSource = arrayList; dataGridViewMessages.Refresh(); And my dataGridView displays correctly.
Later in my code i wish to clear my dataGridView, so i go:
arrayList.Clear(); dataGridView.Refresh();but it reports an error.
Whats the correct way to clear my DataGridView
Later I wish to fill my datGridView again.
Thanks. Paul.

Little help with DataGridView please
Daniel2005
MadAboucC, i tried your code which works but i lose my column styles i created in VS2005 properties window/columns collection when i re-populate my dataGridView.
Any ideas
Horst Klein
By the way, Did you try:
dataGridView.Rows.Clear();
arrayList.Clear();
This will clear the rows keeping the data source as the arraylist!
Hope it helps!
ClydeCoulter
I tried using a BindingList and it works great. thank you.
ZopoStyle
Barron Gillon
I've been trying this for a while now, and here's what i got:
It seems that when you detach the DataSource property, all columns and column styles are cleared, so to repopulate, you will have to recreate the DataGridView control's styles all over again. For example, I created a class Student and stored multiple instances in an array list, added a DataGridView control and two buttons. The first will simply clear the DGV and the second attempts to repopulate it without loosing the format and styles. The student class has a name, an id, a guid, a datereg and an acummAverage (sample properties). These properties map to five columns. So i designed the DGV in design view like you did, added five columns with header names and properties. The problem was the order of the population when trying to re-populate the columns. I went to the Form1.Designer.cs file (expand your form's class file in solution explorer and you'll see it) and copied the lines to recreate the DataGridView columns collection from there and pasted it into a method, then, at repopulation i called that method to recreate the columns collection of the DGV. This and only this worked! Hope that this example helps you in your case and can be easily transfered (at least the methodology it self). I included the full code for you to see:
In the main form of you application, switch to code view and add the following lines to the Form1 class:
private ArrayList dsArr = new ArrayList(20);public Form1() { for (int i = 0; i < 20; i++) {dsArr.Add(new Studnet());}
InitializeComponent();
dataGridView1.DataSource = dsArr;
}
bobslayer
I encounter the same error. Any suggestions, please
DmitryMS
Try this, when assigning null to the DataSource, make a backup of your styles and then when repopulating use the backup to reasign the styles. Another method is to create a method that will set the styles for a datagrid view and maybe another to store them.
An example of the first method would be:
DataGridViewHeaderBorderStyle dgvhbs = dgv.ColumnHeadersBorderStyle;DataGridViewCellStyle dgvcs = dgv.ColumnHeadersDefaultCellStyle;
int dgvhh = dgv.ColumnHeadersHeight;
DataGridViewColumnHeadersHeightSizeMode dgvchhm = dgv.ColumnHeadersHeightSizeMode;
DataGridViewColumn[] cols = new DataGridViewColumn[dgv.Columns.Count];
dgv.Columns.CopyTo(cols, 0);
---------------------------------------------
dgv.DataSource = null;
//.......
//Your code
michaelleewebb
I tried it and i received error: Rows collection cannot be programmatically cleared when the DataGridView control is data-bound to anything else than an IBindingList that supports change notification and allows deletion.
George Shear
KitGreen
What's the error that appeared
Try:
dataGridView.DataSource = null;
arrayList.Clear();
//You might not need this so add it only if the first two lines alone won't work
dataGridView.Refresh();
Hope This helps!
Michael Weingartner
O.K. I solved it:
BindingSource.DataSource =
null;DataSet.Clear();
GridView.DataSource =
null;GridView.Rows.Clear();
GridView.Refresh();
GetCode
Eureka!
I fought with a similar problem for two days. I needed to clear a DataGridView then repopulate it later. All attempts to nullify the data source, or fiddle with the DataTable, or repopulate it using a SELECT statement that returned blank columns... all produced undesirable results. I almost wrote my own initializer like one poster here suggested, but refused to believe I had to go through that much trouble. The answer was pleasingly simple.
Here's what worked:
foreach (DataGridViewRow row in myDataGridView.Rows)
{
}