Solved

GridView.EditIndex always shows -1

Posted on 2012-03-14
2
352 Views
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"
            >
                <Columns>
                    <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>  

                    <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>       
                     </EditItemTemplate>
                     </asp:TemplateField> 

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

                    <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>       
                    </EditItemTemplate>
                     </asp:TemplateField>  

                    <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"> 
                    <ItemTemplate> 
                    <asp:ImageButton ID="ibtnDeny" runat="server" ImageUrl="..\..\images\delete.jpg" OnClick="ibtnDeny_Click" RowIndex='<%# DataBinder.Eval(Container.DataItem, "id") %>'  CommandName="deny" ImageAlign="AbsMiddle">
                    </asp:ImageButton>
                    </ItemTemplate>
                     </asp:TemplateField> 
                         
                    <asp:TemplateField HeaderText="Approve" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"> 
                    <ItemTemplate> 
                    <asp:ImageButton ID="ibtnApprove" runat="server" ImageUrl="..\..\images\green_drill.jpg" OnClick="ibtnApprove_Click" RowIndex='<%# DataBinder.Eval(Container.DataItem, "id") %>'  CommandName="approve" ImageAlign="AbsMiddle">
                    </asp:ImageButton>
                    </ItemTemplate>
                     </asp:TemplateField>                 

                </Columns>
            </asp:GridView>

Open in new window

0
Comment
Question by:dpicco
2 Comments
 
LVL 39

Accepted Solution

by:
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)
0
 
LVL 3

Author Closing Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now