How clear GridView in asp.net?
I resolved the problem, in the moment than clear the GridView with
DataTable ds = new DataTable();
ds = null;
grdResultados.DataSource = ds;
grdResultados.DataBind();
this clear the GridView but dont clear the names of columns, and this was the error, also have to clean the names of the columns. To remove the columns:
for (int i = 0; grdResultados.Columns.Count > i; )
{
grdResultados.Columns.RemoveAt(i);
}
and in the method of load th GridView must be generate the columns automatically with this property:
grdResultados.AutoGenerateColumns = true;
I leave this in case anyone else has the same problem
Just use null value:
grdResultados.DataSource = null;
grdResultados.DataBind();
try this
grdResultados.DataSource = null;
or
grdResultados.Rows.Clear();
then rebind the gridview
int gvHasRows = grdResultados.Rows.Count;
if (gvHasRows > 0)
{
grdResultados.Columns.Clear();
grdResultados.DataBind();
}
First check that there is data in the Gridview before trying to clear. Rows has no Clear function.