Create Dynamic GridView with Paging in ASP.Net using C# and VB.Net

Last Reply on Dec 23, 2014 07:04 AM By Shashikant

Posted on Dec 23, 2014 02:35 AM

I have created a dynamic gridview with constructor builder, and I would like to add an event gridview paging, when I managed to make the event. I do not know how to call my gridview paging event on the index .. more details I can see my coding ..

thanks in advance

protected void Page_Load(object sender, EventArgs e)
{
 GridView obj = new GridView();
 obj.DataSource = ds;
 obj.DataBind();
 obj.AutoGenerateColumns = true;
 obj.ID = name;
 obj.Attributes.Add("ID", name);
 obj.CssClass = "grid freeze";
 obj.AutoGenerateColumns = false; obj.AllowPaging = true;
 obj.PageIndexChanging += new GridViewPageEventHandler(obj_PageIndexChanging);
}
protected void obj_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
}
Posted on Dec 23, 2014 07:04 AM Modified on on Dec 24, 2014 09:42 AM

This way you can bind gridview with paging.

C# code

Namespaces

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

C#

GridView gvCustomers = new GridView();
protected void Page_Load(object sender, EventArgs e)
{
    this.PopulateCustomers();
}

private void PopulateCustomers()
{
    gvCustomers.ID = "gvCustomers";
    gvCustomers.AllowPaging = true;
    gvCustomers.PageSize = 2;
    gvCustomers.PageIndexChanging += new GridViewPageEventHandler(GridView_PageIndexChanging);
    this.form1.Controls.Add(gvCustomers);
    string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                DataSet ds = new DataSet();
                da.Fill(ds);
                gvCustomers.DataSource = ds;
                gvCustomers.DataBind();
            }
        }
    }
}
    
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    gvCustomers.PageIndex = e.NewPageIndex;
    PopulateCustomers();
}
 VB.Net Code
 Namespaces
Imports System.Data.SqlClient
Imports System.Data

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Me.PopulateCustomers()
End Sub

Private Sub PopulateCustomers()
    gvCustomers.ID = "gvCustomers"
    gvCustomers.AllowPaging = True
    gvCustomers.PageSize = 2
    'gvCustomers.PageIndexChanging += New GridViewPageEventHandler(AddressOf GridView_PageIndexChanging)
    AddHandler gvCustomers.PageIndexChanging, AddressOf GridView_PageIndexChanging
    Me.form1.Controls.Add(gvCustomers)
    Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand("SELECT * FROM Customers", con)
            Using da As New SqlDataAdapter(cmd)
                Dim ds As New DataSet()
                da.Fill(ds)
                gvCustomers.DataSource = ds
                gvCustomers.DataBind()
            End Using
        End Using
    End Using
End Sub

Protected Sub GridView_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
    gvCustomers.PageIndex = e.NewPageIndex
    PopulateCustomers()
End Sub

Screenshot

Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
 
You can download database SQL from here.