DataGridView.Clear()

C#.Net

C# Problem Overview


Here comes the trouble. I want to delete all rows from datagridview. This how i add rows:

private void ReadCompleteCallback(object clientHandle, Opc.Da.ItemValueResult[]     results)
{
    foreach (Opc.Da.ItemValueResult readResult in results)
    {
        dataGridView1.Invoke(new MethodInvoker(() => dataGridView1.Rows.Add(readResult.ItemName, readResult.Quality, readResult.Timestamp,readResult.Value)));        
    }
}                              

And its how i clear gridview:

private void treeView1_SelectionsChanged(object sender, EventArgs e)
{
    dataGridView1.Rows.Clear();
    items = new Opc.Da.Item[treeView1.SelectedNodes.Count]; 
    foreach (TreeNode x in treeView1.SelectedNodes) {
        items[treeView1.SelectedNodes.IndexOf(x)] = new Opc.Da.Item();
        items[treeView1.SelectedNodes.IndexOf(x)].ItemName = x.Text; 
    }
           
    group.AddItems(items);
    group.Read(group.Items, 123, new Opc.Da.ReadCompleteEventHandler(ReadCompleteCallback), out req);
}

in debug i see that dataGridVIew1.Rows.Count=0, but on form, grid doesnt become clear. what a point? on each selection in tree, i want to see new rows in table.

C# Solutions


Solution 1 - C#

I'm betting you just need to refresh the datagrid. Try this:

dataGridView1.Rows.Clear();
dataGridView1.Refresh();

If this works... you might want to rethink this part of your application.

Solution 2 - C#

If I remember correctly, I set the DataSource property to null to clear the DataGridView:

datagridview.DataSource = null;

Solution 3 - C#

To clear the datagridview write the following code:

dataGridView1.DataSource=null;

Solution 4 - C#

Basically below code line is only useful in data binding scenarios

datagridview.DataSource = null; 

otherwise below is the used when no data binding and datagridview is populated manually

dataGridView1.Rows.Clear();
dataGridView1.Refresh();

So, check first what you are doing.

Solution 5 - C#

If you want clear a dataGridView not binded to DataSource but manually loaded you can use this simple code:

datagridview.Rows.Clear();
datagridview.Columns.Clear();

Solution 6 - C#

all you need to do is clear your datatable before you fill it... and then just set it as you dgv's datasource

Solution 7 - C#

I have this piece of code i hope it may help u.

int numRows = dgbDatos.Rows.Count;    
for (int i = 0; i < numRows; i++)
{
    try
    {    
        int max = dgbDatos.Rows.Count - 1;
        dgbDatos.Rows.Remove(dgbDatos.Rows[max]);
        
    }
    catch (Exception exe)
    {
        MessageBox.Show("You can´t delete A row " + exe, "WTF",
        MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

Solution 8 - C#

Simply if your are trying to rebind your Data Grid View just code this:

DataGridView1.DataSource = DataSet.Tables["TableName"];

OR

DataGridView.DataSource = DataTable;

else if you trying to to clear your Data Grid View just code this:

DataGridView.DataSource = null;
DataGridView.Rows.Clear();
DataGridView.Refresh();

and add any method or event to bind Data Grid View Again below this line of code....

Solution 9 - C#

try setting RowCount to 0(allowuserstorows should be false), along with calling clear

Solution 10 - C#

This is one way of doing it:

datagridview.DataSource = null;
datagridview.Refresh();

I hope it works for you because it is working for me.

Solution 11 - C#

try:

datagrid.DataSource = null;
datagrid.DataBind();

Basically you will need to clear the datasource your binding to the grid.

Solution 12 - C#

Try this Method

dataGridView1.DataSource = dt;
dt.Rows.Clear();

Solution 13 - C#

dataGridView1.DataSource=null;

Solution 14 - C#

Set the datasource property to an empty string then call the Clear method.

Solution 15 - C#

I don't like messing with the DataSource personally so after discussing the issue with an IT friend I was able to discover this way which is simple and doesn't effect the DataSource. Hope this helps!

foreach (DataGridViewRow row in dataGridView1.Rows) 
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        cell.Value = "";
    }
}

Solution 16 - C#

If your DataGridView does not have any DataSource the solution does not come by manipulating it.

You will always have an empty row if you have the AllowUserToAddRows property set to true.

Put AllowUserToAddRows = false if you don't need permise this.

Solution 17 - C#

I know it sounds crazy, but I solved this issue by changing the datagrid.visible property to false and then to true. It causes a very small blink, but it works for me (at least for now).

Solution 18 - C#

Try this Code

private void button_Click(object sender, EventArgs e) {
    if (this.dgInv.DataSource != null) {
        this.dgInv.DataSource = null;
    } else {
        this.dgInv.Rows.Clear();
    }
}

Solution 19 - C#

Try this

DataGridView1.DataSource=ds;
ds.Clear();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionebaView Question on Stackoverflow
Solution 1 - C#NitroxDMView Answer on Stackoverflow
Solution 2 - C#IAbstractView Answer on Stackoverflow
Solution 3 - C#msz900View Answer on Stackoverflow
Solution 4 - C#Syed Rizwan ShahzadView Answer on Stackoverflow
Solution 5 - C#daniele3004View Answer on Stackoverflow
Solution 6 - C#Abdulqadir TravadyView Answer on Stackoverflow
Solution 7 - C#Ivan ArandaView Answer on Stackoverflow
Solution 8 - C#Obaid Ul HaqueView Answer on Stackoverflow
Solution 9 - C#Vinay B RView Answer on Stackoverflow
Solution 10 - C#Yousef ImranView Answer on Stackoverflow
Solution 11 - C#user2272525View Answer on Stackoverflow
Solution 12 - C#MajorView Answer on Stackoverflow
Solution 13 - C#Owaix AnsariView Answer on Stackoverflow
Solution 14 - C#DavidView Answer on Stackoverflow
Solution 15 - C#EricView Answer on Stackoverflow
Solution 16 - C#Leonardo HernándezView Answer on Stackoverflow
Solution 17 - C#Edson LopezView Answer on Stackoverflow
Solution 18 - C#Bijumon MathewView Answer on Stackoverflow
Solution 19 - C#Irfan NoorView Answer on Stackoverflow