Gridview Paging using C#

In ASP.Net 3.0 GridView is most often used to display the data retrieved from the database in Tabular form with features of Gridview like data paging, sorting and auto formats.

You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with paging enabled.

First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.

<asp:GridView id="GridView1" runat="server"></asp:GridView>

To enable the paging in GridView control select True from the dropdown list of AllowPaging property of GridView control as shown in the above image.

It will add AllowPaging="True" in HTML source code of Gridview.

Next step is to bind the data with Gridview control and handle the GridView paging event.

To bind the PageIndexChanging of GridView control, double click on the PageIndexChanging event in the properties of Gridview. It will add the event in HTML source code as well as C# code.

To bind the PageIndexChanging of GridView control, double click on the PageIndexChanging event in the properties of Gridview. It will add the event in HTML source code as well as C# code.


<asp:GridView runat="server" ID="GridView1" DataKeyNames="EmpID" DataSourceID="EmpDetails"

AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"  OnRowDataBound="gvEmpdetails_RowDataBound" OnPageIndexChanging="gvEmpdetails_OnPageIndexChanging" PageSize="10">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkhead" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" SortExpression="EmpSal" />
<asp:BoundField DataField="EmpBranch" HeaderText="EmpBranch" SortExpression="EmpBranch" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" SortExpression="EmpCode" />

</Columns>
</asp:GridView>

In the Code Behind Page OnPageIndexChanging event As Follows:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

                 gdChildDetails.PageIndex = e.NewPageIndex;

               //Bind Gridview Data....

                 SqlConnection scon = null;

                SqlDataAdapter sda = null;

                DataSet ds = null;

                scon = new SqlConnection(@"Data Source=TAPA-PC;user      Id=sa;password=123;DataBase=Customer");

                sda = new SqlDataAdapter("select * from EmpDeduction", scon);

                ds = new DataSet();

                sda.Fill(ds, "emp");

                if (ds.Tables[0].Rows.Count > 0)


                {

                    gdChildDetails.DataSource = ds;
                    gdChildDetails.DataBind();

                }

      }


Out Put ScreenShots :
                                                                            Page1
                                                                            Page 2
                                                                              Page 3

1 Responses to “Gridview Paging using C#”

Unknown said...
12 November 2012 at 00:35

Nice article dapfor provide a net grid which is very useful visit dapfor. com


Post a Comment

Labels

Topics