Click here to Skip to main content
15,915,777 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I Have a grid view ,which contains a huge data.In the 18th column I have a delete button in my grid view.for confirmation message I have used gridview RowDataBound event(the code for this is below). Its working fine.But when I used Gridview Paging it is throwing the exception saying that "An exception of type System.ArgumentOutOfRangeException' occurred in System.Web.dll but was not handled in user code.
Additional information: Specified argument was out of the range of valid values."

Please anyone help me out from this problem.My codes are below..............

C#


//code of gridview in aspx page
XML
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="False" DataKeyNames="ID,Created"
                    CssClass="table table-hover table-striped" ForeColor="Black" GridLines="None" OnRowCommand="gvEmployees_RowCommand"
                    OnRowDataBound="gvEmployees_RowDataBound" AllowPaging="True" OnPageIndexChanging="gvEmployees_PageIndexChanging">
                    <Columns>
                        <asp:ButtonField ButtonType="Button" CommandName="detailsOfEmployee" Text="Details">
                            <ControlStyle CssClass="btn btn-info btn-xs" />
                        </asp:ButtonField>
                        <asp:BoundField DataField="ID" HeaderText="ID" Visible="False" />
                        <asp:BoundField DataField="Code" HeaderText="Code" />
                        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                        <asp:BoundField DataField="MiddleName" HeaderText="Middle Name" />
                        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                        <asp:BoundField DataField="DateOfBirth" HeaderText="Date Of Birth" Visible="False" />
                        <asp:BoundField DataField="EmailID" HeaderText="Email ID" Visible="False" />
                        <asp:BoundField DataField="UserName" HeaderText="User Name" />
                        <asp:BoundField DataField="EncryptedPassword" HeaderText="Encrypted Password" Visible="False" />
                        <asp:BoundField DataField="PasswordSalt" HeaderText="Password Salt" Visible="False" />
                        <asp:BoundField DataField="DateOfJoining" HeaderText="Date Of Joining" Visible="False" />
                        <asp:BoundField DataField="Designation" HeaderText="Designation" />
                        <asp:BoundField DataField="UserRole" HeaderText="UserRole" Visible="False" />
                        <asp:BoundField DataField="IsActive" HeaderText="IsActive" Visible="False" />
                        <asp:BoundField DataField="Created" HeaderText="Created" Visible="False" />
                        <asp:BoundField DataField="LastUpdated" HeaderText="Last Updated" Visible="False" />
                        <asp:ButtonField ButtonType="Button" CommandName="editEmployee" Text="Edit">
                            <ControlStyle CssClass="btn btn-info btn-xs" />
                        </asp:ButtonField>
                        <asp:ButtonField ButtonType="Button" CommandName="deleteEmployee" Text="Delete">
                            <ControlStyle CssClass="btn btn-info btn-xs" />
                        </asp:ButtonField>
                    </Columns>
                </asp:GridView>

//code of RowDatabpond event in .CS page
C#
protected void gvEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     foreach (Button button in e.Row.Cells[18].Controls.OfType<Button>())
     {
         if (button.CommandName == "deleteEmployee")
         {
             button.Attributes["onclick"] = "if(!confirm('Do you want to delete ?')){ return false; };";
             //button.Attributes.Add("onclick", "return confirm('Do you want to delete the record?');");

         }

     }
 }


//code for gridview paging event
C#
protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvEmployees.PageIndex = e.NewPageIndex;
       gvEmployees.DataBind();
   }
Posted

1 solution

call the function that fill the GridView gvEmployees after the PageIndexChanged Event

C#
protected void gvEmployees_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       gvEmployees.PageIndex = e.NewPageIndex;
     this.fillGridView1();
   }
private void fillGridView1() 
{ 
gvEmployees.DataSource = datasourcename;
 gvEmployees.DataBind(); 
}
 
Share this answer
 
Comments
Member 11246037 19-Jan-15 10:11am    
I have tried the same Mr.Deepu S Nair.But its showing the same error.
I thing for the first 10 row its counting the button column no. as Page Size:10.But when it is reading for 11th row its throwing the exception.AS the page has only 10 rows but the gridview has many rows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900