Wednesday, April 20, 2011

Getting the current row in GridView Row Command Event.

In row command if you want to get the current row or its index, It is not directly possible. There is two way, one is on row created event on the link button(suppose, it is used for row command) save row index in link button command argument attribute.

And get the argument from row command event using e.CommandArgument

But if you want to send another value as a command argument then it creates problem. To solve this just use this code

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Here link button (or any source) that cause to enter in row command event. Now you have selected row so you don’t need any index and directly access any row variable as sample code is given below
Label lblProdId = (Label)row.FindControl(“lblproductId”);

So whole code is just two line. Below I write whole code

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Label myLabel = (Label)row.FindControl("myLabel");

No comments: