Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a row from GridView?

I am using GridView control in asp.net 2005 c# 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();
like image 949
Kartik Avatar asked Feb 26 '09 19:02

Kartik


People also ask

How can delete selected row in GridView in ASP NET?

Try this: Gridview has a property called SelectedIndex. If you want to unselect any rows then set this property to -1.

How do I delete a row from GridView in Windows app?

Right click to select row in dataGridView The first thing you will want to do is to drag a contextMenuStrip from your toolbox to your form. Then you create a menu item "Delete Row" in the contextMenuStrip.


2 Answers

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.

like image 162
TheTXI Avatar answered Sep 20 '22 14:09

TheTXI


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.

like image 30
atfergs Avatar answered Sep 21 '22 14:09

atfergs