Hello:
I was working last night around 3:00 AM, and I found out that in my GridView Column's list, I have added a TemplateField of type LinkButton with CommandName=”EditRec”, in such a way, when the Edit link button is clicked, I want to access the selected GridViewRow, then populate a web form on the same page, and without having to show the “Update” and “Cancel” buttons on the GridView.
I did that, but the problem was now how to access the GridViewRow in the RowCommand method?
I used the “SelectedRow” property but I kept having “Object Reference not ....” I didn't know in fact.
I tried to use GridView1.Rows[e.CommandArgument], but the DataKey I set for the GridView was of type String, so cannot use that either.
I finally was able to use the following and it works fine for me:
if (e.CommandName.Equals("EditRec"))
{
string MediaIndex = e.CommandArgument.ToString();
// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
string _mediaType = selectedRow.Cells[0].Text;
string _CheckOut = selectedRow.Cells[1].Text;
}
The way the code works if as follows:
e.CommandSource -> LinkButton
((Control)e.CommandSource).NamingContainer -> GridViewRow
Then, simply use the Cells property inside the GridViewRow to access all cells.
Hope that Helps,
Regards
Tags: ASP.NET 2.0 - General