views:

14867

answers:

4

Hello,

I am using asp.net 2005 c# using gridview control.

How can i delete particular row from gridview.

I wrote 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();
A: 

Delete the row from the table dtPrf_Mstr rather than from the grid view.

Welbog
+1  A: 

The default answer is to remove the item from whatever collection you're using as the GridView's DataSource.

If that option is undesirable then I recommend that you use the GridView's RowDataBound event to selectively set the row's (e.Row) Visible property to false.

Ken Browning
+3  A: 

You're deleting the row from the gridview and then rebinding it to the datasource (which still contains the row). Either delete the row from the datasource, or don't rebind the gridview afterwards.

orthod0ks
Wow...that was almost word for word identical to what I just posted (48 secs apart)
TheTXI
+1 for beating me to it
TheTXI
it's work thanks lot
Kartik
+6  A: 

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.

TheTXI
it's work thanks lot
Kartik
Wow, that was close. +1 for beating me
orthod0ks