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


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


GridView gvCustomers = new GridView();
protected void Page_Load(object sender, EventArgs e)

private void PopulateCustomers()
    gvCustomers.ID = "gvCustomers";
    gvCustomers.AllowPaging = true;
    gvCustomers.PageSize = 2;
    gvCustomers.PageIndexChanging += new GridViewPageEventHandler(GridView_PageIndexChanging);
    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();
                gvCustomers.DataSource = ds;
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    gvCustomers.PageIndex = e.NewPageIndex;
 VB.Net Code
Imports System.Data.SqlClient
Imports System.Data


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
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
    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()
                gvCustomers.DataSource = ds
            End Using
        End Using
    End Using
End Sub

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


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.