GridView.EditIndex always shows -1

Posted on 2012-03-14
Last Modified: 2012-03-14
Hello experts,
When I edit a row in my GridView, the EditIndex always shows -1 in my gvBonuses_RowDataBound event. Can anyone tell me why this is happening? Gridview code is below:

            <asp:GridView ID="gvBonuses" ValidationGroup="Edit" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="id" DataSourceID="sdsBonuses" CssClass="mGrid" 
            OnRowDeleting="gvBonuses_RowDeleting" OnRowUpdated="gvBonuses_RowUpdated" 
            OnRowEditing="gvBonuses_RowEditing" OnRowDeleted="gvBonuses_RowDeleted" 
            OnRowUpdating="gvBonuses_RowUpdating" OnRowDataBound="gvBonuses_RowDataBound"
                    <asp:CommandField ShowEditButton="True" ValidationGroup="Edit" CausesValidation="true"/>
                    <asp:BoundField DataField="id" ReadOnly="True" Visible="false" />
                    <asp:BoundField DataField="employee_id" HeaderText="Employee ID" ReadOnly="True" 
                        SortExpression="employee_id" Visible="false"/>
                    <asp:BoundField DataField="name" HeaderText="Payee Name" ReadOnly="True" 
                        SortExpression="name" />
                    <asp:BoundField DataField="pay_code_desc" HeaderText="Pay Code" ReadOnly="True" 
                        SortExpression="pay_code_desc" />
                    <asp:BoundField DataField="gl_account" HeaderText="GL Account" ReadOnly="True" 
                        SortExpression="gl_account" />

                    <asp:TemplateField HeaderText="Loc #"> 
                    <ItemTemplate> <asp:Label ID="lblLocItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "location_num") %>'></asp:Label> </ItemTemplate>
                    <EditItemTemplate> <asp:DropDownList ID="ddlLocEdit" AppendDataBoundItems="true" runat="server" DataTextField="loc" DataValueField="loc" Text='<%# DataBinder.Eval(Container.DataItem, "location_num") %>'>
                    </asp:DropDownList> </EditItemTemplate>

                    <asp:TemplateField HeaderText="Amount"> 
                    <ItemTemplate> <asp:Label ID="lblAmount" runat="server" DataFormatString="{0:c}" Text='<%# DataBinder.Eval(Container.DataItem, "amount","{0:c}") %>'></asp:Label> </ItemTemplate>
                    <EditItemTemplate> <asp:TextBox ID="txtAmount" MaxLength="7" Width="75px" DataField="amount" DataFormatString="{0:N2}" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "amount","{0:N2}") %>'></asp:TextBox> 
                                 <asp:RangeValidator ID="rvGridAmount" ValidationGroup="Edit" runat="server" style="color:red;" ErrorMessage=" * Bonus Amount must be less than $10,000" ControlToValidate="txtAmount" MaximumValue="9999.99" MinimumValue=".01" Visible="True" Type="Currency" EnableClientScript="true" Display="Dynamic"></asp:RangeValidator>        
                                 <asp:RequiredFieldValidator ValidationGroup="Edit" style="color:red;" ID="rfvGridAmount" runat="server" ErrorMessage=" * You must enter a Bonus Amount" ControlToValidate="txtAmount" BackColor="White" Visible="True" EnableClientScript="true" Display="Dynamic"></asp:RequiredFieldValidator>       

                    <asp:TemplateField HeaderText="Driving Category"> 
                    <ItemTemplate> <asp:Label ID="lblCategoryItem" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "category") %>'></asp:Label> </ItemTemplate>
                    <EditItemTemplate> <asp:DropDownList ID="ddlCategoryEdit"  runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "category") %>'>
                <asp:ListItem Text="Drive Overall Sales" Value="Drive Overall Sales" />
                <asp:ListItem Text="Drive Heritage Production" Value="Drive Heritage Production" />
                <asp:ListItem Text="Drive Atneed Merchandise" Value="Drive Atneed Merchandise" />
                <asp:ListItem Text="Drive PAF Production" Value="Drive PAF Production" />
                <asp:ListItem Text="Other" Value="Other" /> 
                    </asp:DropDownList> </EditItemTemplate>

                    <asp:TemplateField HeaderText="Explanation"> 
                    <ItemTemplate> <asp:Label ID="lblExplanation"  runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "explanation") %>'></asp:Label> </ItemTemplate>
                    <EditItemTemplate> <asp:TextBox ID="txtExplanation"  MaxLength="50" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "explanation") %>'></asp:TextBox> 
                                <asp:RequiredFieldValidator ValidationGroup="Edit" style="color:red;" ID="rfvGridExplanation" runat="server" ErrorMessage=" * You must enter an Explanation" ControlToValidate="txtExplanation" BackColor="White" Visible="True" EnableClientScript="true" Display="Dynamic"></asp:RequiredFieldValidator>       

                    <asp:BoundField DataField="submitter_name" HeaderText="Requested By" ReadOnly="True" 
                        SortExpression="submitter_name" />
                    <asp:BoundField DataField="created_date" HeaderText="Request Date" ReadOnly="True" 
                        SortExpression="created_date" DataFormatString="{0:d}"/>
                    <asp:BoundField DataField="bonus_status" HeaderText="Status" ReadOnly="True" 
                        SortExpression="bonus_status" />

                    <asp:BoundField DataField="approved_denied_by" HeaderText="Approved/Denied By" ReadOnly="True" 
                        SortExpression="approved_denied_by" />
                    <asp:BoundField DataField="approved_denied_date" HeaderText="Approved/Denied Date" ReadOnly="True" 
                        SortExpression="approved_denied_date" DataFormatString="{0:d}"/>
                    <asp:TemplateField HeaderText="Deny" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"> 
                    <asp:ImageButton ID="ibtnDeny" runat="server" ImageUrl="..\..\images\delete.jpg" OnClick="ibtnDeny_Click" RowIndex='<%# DataBinder.Eval(Container.DataItem, "id") %>'  CommandName="deny" ImageAlign="AbsMiddle">
                    <asp:TemplateField HeaderText="Approve" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"> 
                    <asp:ImageButton ID="ibtnApprove" runat="server" ImageUrl="..\..\images\green_drill.jpg" OnClick="ibtnApprove_Click" RowIndex='<%# DataBinder.Eval(Container.DataItem, "id") %>'  CommandName="approve" ImageAlign="AbsMiddle">


Open in new window

Question by:dpicco
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 40

Accepted Solution

Kyle Abrahams earned 500 total points
ID: 37722411
how are you binding the data?

The most common cause of this is that you're binding this outside the

if (!Page.IsPostBack)

Author Closing Comment

ID: 37722556
Yep that was the problem. Thanks  You're smart.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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