Datagrid Item.ItemIndex is pulling the next row instead of the correct row

Posted on 2007-10-15
Last Modified: 2012-06-21
I am creating a custom web application for our online portal.  I am using C# to code this application and I am having a problem with my data grid.  I can get the data to display on the datagrid, but when I select the edit command the value (Student ID Number) is one off.  For example if I select the first item on the datagrid to edit and pulls the key information of the next one down on the datagrid.

Here is the datagrid code.  Please note that the groupedgrid is a custom function using the datagrid that was developed by our online portal product developer but uses the same properties:

      <common:groupedgrid id="grdSearchResults" RenderGroupHeaders="True" runat="server" GridLines="None"
            HeaderRightColumns="(Collection)" allowsorting="True" allowpaging="True" PagerStyle-Mode="NumericPages"
            PagerStyle-HorizontalAlign="Right" RenderTableFooters="True" ShowFooter="True" DataKeyField="ID_NUM">
                  <common:EditButtonColumn ToolTipKey="" TextKey="" HeaderText="View"></common:EditButtonColumn>
                  <asp:TemplateColumn SortExpression="NM.ID_NUM" HeaderText="ID Number">
                              <%# DataBinder.Eval(Container.DataItem,"ID_NUM") %>
                  <asp:TemplateColumn SortExpression="NM.FIRST_NAME" HeaderText="First Name">
                              <%# DataBinder.Eval(Container.DataItem,"FIRST_NAME") %>
                  <asp:TemplateColumn SortExpression="NM.LAST_NAME" HeaderText="Last Name">
                              <%# DataBinder.Eval(Container.DataItem,"LAST_NAME") %>
                  <asp:TemplateColumn HeaderText="DOB">
                              <%# DataBinder.Eval(Container.DataItem,"DOB") %>
                  <asp:TemplateColumn SortExpression="BM.GENDER" HeaderText="Gender">
                              <%# DataBinder.Eval(Container.DataItem,"GENDER") %>
            <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>

Here is the code behind for the edit function:

            private void grdSearchResults_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
                  this.ParentPortlet.PortletViewState["ID_NUM"] = grdSearchResults.DataKeys[e.Item.ItemIndex].ToString();
                  //Label1.Text = grdSearchResults.DataKeys[e.Item.ItemIndex].ToString();

I have used this method in the past and it has worked perfectly.

Thanks for any assistance!

Question by:blouckswwu
    LVL 12

    Accepted Solution

    Well, you could always just subtract 1 from grdSearchResults.DataKeys[e.Item.ItemIndex] if it's always only one off.  I know that's not really a solution, but it's a means to an end.

    Author Comment

    Actually I just rebuilt the datagrid and it works now.  I have ran into stuff like this before in VS 2003.  If all else fails...just redo it. :-)

    I actually initially set it to subtract 1, but that makes me a little nervous because of potential inconsistencies (if it all of a sudden decided to work like it was designed).

    I guess since you were the only one to respond I'll give you the points!

    Have a good day and thanks for the response!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now