Here comes the trouble. I want to delete all rows from datagridview. This how i add rows:
private void ReadCompleteCallback(object clientHandle, Opc.Da.ItemV
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();
}
}
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 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 = "";
}
}
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.