narasiman rao

narasiman rao

  • NA
  • 519
  • 768.7k

Delete the partiuclar row in gridview in asp.net using c#

May 28 2015 11:32 PM
       I am deleting partiuclar row in gridview.

Run mode as follows


Waitlistid Course Batchdate
1 AFF 25 Aug 2014
2 MFA 26 Sep 2015
3 ERS 20 Mar 2012
4 ROC 19 Jul 2012


My code as follows

protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{

int index = Convert.ToInt32(e.RowIndex);
DataTable dt = ViewState["dt"] as DataTable;
dt.Rows[index].Delete();
ViewState["dt"] = dt;
BindGrid();
}


protected void BindGrid()
{
GridView1.DataSource = ViewState["dt"] as DataTable;
GridView1.DataBind();
}

When i delete the 4th row in gridview in row mode shows error as follows

Object reference not set to an instance of an object.

The above error shows in below line as follows

dt.Rows[index].Delete();

please help me. what is the problem in my abvoe code. 

Answers (2)