How to get the value from a GridView column on RowDeleting

The Situation:

You need to get the value of one of the columns in the row you are deleting in a GridView to do some processing before the row is deleted.

A Solution:

When using the RowDeleting method, you cannot use e.Row.Cells[1].Controls[1] to get the control in that cell like you can in the RowCreated method. Instead, you can simply access the text of the desired cell and convert it to whatever datatype you need, like this:

protected void gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
	// You get the Key (identity value) of the row like this...
	string userName = gridview1.DataKeys[e.RowIndex].Value.ToString();
	
	// And you can get the value of any other cell in the row like this... 
	// (this would get the value in the 4th cell)
	string cellval = gridview1.Rows[e.RowIndex].Cells[3].Text;
	
	// DO MORE PROCESSING HERE
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s