CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Question 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?

  2. #2
    Join Date
    Dec 2011
    Posts
    61

    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();

  3. #3
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    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."

  4. #4
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    Re: CurrentRowIndex problem in Gridview

    It also seems as if this event fires before the row is inserted...

  5. #5
    Join Date
    Dec 2011
    Posts
    61

    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 %>" />

  6. #6
    Join Date
    Jun 2001
    Location
    Denmark
    Posts
    453

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured