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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 33
Please explain the parts of these 2 LINQ expressions 3 53
C# LINQ ForEach() question 6 48
SSRS Deployment problem 5 63
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

789 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