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.
I required it to clear all rows of datagridview on button click event. dataGridView1. Rows. Clear();
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.
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.
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.
If I remember correctly, I set the DataSource property to null to clear the DataGridView:
datagridview.DataSource = null;
To clear the datagridview
write the following code:
dataGridView1.DataSource=null;
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.
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();
all you need to do is clear your datatable before you fill it... and then just set it as you dgv's datasource
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);
}
}
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....
try setting RowCount to 0(allowuserstorows should be false), along with calling clear
This is one way of doing it:
datagridview.DataSource = null;
datagridview.Refresh();
I hope it works for you because it is working for me.
try:
datagrid.DataSource = null;
datagrid.DataBind();
Basically you will need to clear the datasource your binding to the grid.
Try this Method
dataGridView1.DataSource = dt;
dt.Rows.Clear();
dataGridView1.DataSource=null;
Set the datasource property to an empty string then call the Clear method.
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 = "";
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With