'How do delete All rows at DataGridView?
this is code :
BindingSource bs = new BindingSource();
DataTable tbl(string sql)
{
OracleConnection con = new OracleConnection(connectionstring);
OracleDataAdapter adp = new OracleDataAdapter(sql, con);
DataSet ds = new DataSet();
adp.Fill(ds, "tbl");
return ds.Tables["tbl"];
}
void GetData()
{
bs.DataSource = Class1.tbl("select USER_ID ,EMP_NAME as pName,EMP_MOBILE from TBLUSERS");
datagridview1.Columns[0].DataPropertyName = "USER_ID";
datagridview1.Columns[1].DataPropertyName = "pName";
datagridview1.Columns[2].DataPropertyName = "EMP_MOBILE";
datagridview1.DataSource = bs;
}
void ClearAllRows()
{
datagridview1.Rows.Clear();
//The error occurs here
}
The error occurs here How do delete All rows at DataGridView ? my DataGridView is BindingSource
Solution 1:[1]
while (dataGridView1.Rows.Count>0)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}
Solution 2:[2]
Please Do This Code
if(dgvItemDetails.Rows.Count>0) {
do
{
dgvItemDetails.Rows.RemoveAt(0);
}
while (dgvItemDetails.Rows.Count > 0);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Damien |
| Solution 2 | Vincent |
