CurrentRowIndex problem in Gridview
I have an ItemTemplate in my gridview. When I enter the gridview_RowCommand function, I'd like to get my CurrentRowIndex like I do for the normal DataBound columns.
Sadly, the COmmandArguemnt is empty. I can see there's a property for it, but i thought it would be there automatically like normal. Or should I fill it out, and if so, how?
Re: CurrentRowIndex problem in Gridview
The CommandArgument will be automatically set only for ButtonField column. Since you are using ItemTemplate, you need to specify the CommandArgument manually. You may set it in gridview's RowDataBound event:
linkbutton.CommandArgument = e.Row.RowIndex.ToString();
Re: CurrentRowIndex problem in Gridview
Ah...that seems to work. Now I only need to get my controls...
I tried
Code:
if (e.Row.RowIndex < 0) return;
LinkButton VaerdiEdit = (LinkButton)gwAnalyserAddEdit.Rows[e.Row.RowIndex].FindControl("LbVaerdi");
if (VaerdiEdit!=null)
VaerdiEdit.CommandArgument=e.Row.RowIndex.ToString();
but then it complains about "Index was out of range. Must be non-negative and less than the size of the collection."
Re: CurrentRowIndex problem in Gridview
It also seems as if this event fires before the row is inserted...
Re: CurrentRowIndex problem in Gridview
try this:
LinkButton VaerdiEdit = e.Row.Cells[0].Controls[0] as LinkButton;
another way is to specify the CommandArgument in aspx:
<asp:Button ... CommandArgument="<%# Container.DataItemIndex %>" />
Re: CurrentRowIndex problem in Gridview
I solved the problem. I moved the code to the RowCreated event, and I added a class variable int idx that I set in the RowDataBound, and then use in RowCreated. That works!