[c#] DataGridView.Clear()

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.

This question is related to c# .net

The answer is


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.


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....


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.


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


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


This is one way of doing it:

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

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


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

datagridview.DataSource = null;

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);
    }
}

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


Try this

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

To clear the datagridview write the following code:

dataGridView1.DataSource=null;

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();
    }
}

dataGridView1.DataSource=null;

try:

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

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


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


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();

Try this Method

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

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 = "";
    }
}