Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView.Clear()

Tags:

c#

.net

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.

like image 487
eba Avatar asked Sep 19 '10 07:09

eba


People also ask

How to clear DataGridView rows?

I required it to clear all rows of datagridview on button click event. dataGridView1. Rows. Clear();

How to clear DataGridView in c# winform?

If your DataGridView is bounded, "dataGridView1. DataSource = null" is sufficient to clear it. If you want to Refresh it after a new fill, set the DataSource to null and restore it to what it was before, i.e. DataTable or BindingSource.

How to clear data Grid view in asp Net?

Setting datasource to null and then rebinding will clear out everything in the grid. As others mentioned, reset the datasource and bind again for additional content.


16 Answers

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.

like image 118
NitroxDM Avatar answered Oct 05 '22 08:10

NitroxDM


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

datagridview.DataSource = null;
like image 29
IAbstract Avatar answered Oct 05 '22 10:10

IAbstract


To clear the datagridview write the following code:

dataGridView1.DataSource=null;
like image 39
msz900 Avatar answered Oct 05 '22 09:10

msz900


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.

like image 23
Syed Rizwan Shahzad Avatar answered Oct 05 '22 08:10

Syed Rizwan Shahzad


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();
like image 21
daniele3004 Avatar answered Oct 05 '22 08:10

daniele3004


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

like image 26
Abdulqadir Travady Avatar answered Oct 05 '22 09:10

Abdulqadir Travady


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);
    }
}
like image 28
Ivan Aranda Avatar answered Oct 05 '22 10:10

Ivan Aranda


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

like image 33
Obaid Ul Haque Avatar answered Oct 05 '22 08:10

Obaid Ul Haque


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

like image 20
Vinay B R Avatar answered Oct 05 '22 10:10

Vinay B R


This is one way of doing it:

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

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

like image 32
Yousef Imran Avatar answered Oct 05 '22 10:10

Yousef Imran


try:

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

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

like image 26
user2272525 Avatar answered Oct 05 '22 09:10

user2272525


Try this Method

dataGridView1.DataSource = dt;
dt.Rows.Clear();
like image 35
Major Avatar answered Oct 05 '22 09:10

Major


dataGridView1.DataSource=null;
like image 43
Owaix Ansari Avatar answered Oct 05 '22 08:10

Owaix Ansari


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

like image 23
David Avatar answered Oct 05 '22 08:10

David


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 = "";
    }
}
like image 37
Eric Avatar answered Oct 05 '22 09:10

Eric


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.

like image 23
Leonardo Hernández Avatar answered Oct 05 '22 08:10

Leonardo Hernández