How to delete a row from GridView?

Kartik picture Kartik · Feb 26, 2009 · Viewed 148.5k times · Source

I am using GridView control in 2005 using .

How can I delete a particular row from GridView.

I have written the following code. But it's not working...

DataRow dr = dtPrf_Mstr.NewRow();
dtPrf_Mstr.Rows.Add(dr);
GVGLCode.DataSource = dtPrf_Mstr;
GVGLCode.DataBind();

int iCount = GVGLCode.Rows.Count;
for (int i = 0; i <= iCount; i++)
{
    GVGLCode.DeleteRow(i);
}
GVGLCode.DataBind();

Answer

TheTXI picture TheTXI · Feb 26, 2009

You are deleting the row from the gridview but you are then going and calling databind again which is just refreshing the gridview to the same state that the original datasource is in.

Either remove it from the datasource and then databind, or databind and remove it from the gridview without redatabinding.