?
Solved

GridView.EditIndex always shows -1

Posted on 2012-03-14
2
Medium Priority
?
384 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
[X]
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
2 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) 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.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
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…
Suggested Courses

765 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