Welcome to Vishwa's blog - Technology, Spirituality and More...

Dynamic Gridview Generation at run time

In this example, a Gridview is generated at run time with customer list with bound field columns. It also creates a HTML table dynamically. Most of the properties can be easily controlled at run time. I am using Customer Business Object which I created for my earlier posts.

 Page: CustomerDynamicGridView.Aspx

Code Snippet
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="CustomerDynamicGridView.aspx.vb" Inherits="CustomerDynamicGridView" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  3. <html xmlns="">
  4. <head runat="server">
  5. <title>Dynamic Grid View</title>
  6. </head>
  7. <body>
  8. <form id="frmCustomer" runat="server">
  9. <div style="text-align:center">
  10. <asp:PlaceHolder ID="plhCustomer" runat="server">
  11. </asp:PlaceHolder>
  12. </div>
  13. </form>
  14. </body>
  15. </html>

Code Behind

Option Explicit On
Option Strict On
Imports Vishwa.Example.Business
''' <summary>
''' Author :
''' Date : 10/14/2007
''' Class: CustomerDynamicGridView
''' Purpose : To Generate a Dynamic Gridview with Custom Bound Column
''' </summary>
''' <remarks></remarks>
Partial Class CustomerDynamicGridView
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.Load
        IfNot Page.IsPostBack ThenCall PopulateDynamicGrid()
    End Sub
    Private Sub PopulateDynamicGrid()
        Dim gvwGridView AsNew GridView
        Dim tblMain AsNew HtmlTable
        Dim trRow AsNew HtmlTableRow
        Dim tdCell AsNew HtmlTableCell
        Dim lblTitleText AsNew Label
        Dim bizCust AsNew BIZCustomer
        lblTitleText.Text = "Customer List"
        lblTitleText.Font.Bold = True
        lblTitleText.BackColor = Drawing.Color.Navy
        lblTitleText.ForeColor = Drawing.Color.White
        tdCell.Height = "30px"
        With gvwGridView
            .HorizontalAlign = HorizontalAlign.Left
            .BackColor = Drawing.Color.FromName("#ccccff")
            .BorderColor = Drawing.Color.Black
            .CellPadding = 3
            .CellSpacing = 0
            .Font.Name = "Verdana"
            .Font.Size = 8
            .HeaderStyle.Font.Size = 10
            .HeaderStyle.BackColor = Drawing.Color.FromName("#aaaadd")
            .HeaderStyle.VerticalAlign = VerticalAlign.Bottom
            .AlternatingRowStyle.BackColor = Drawing.Color.White
            .HeaderStyle.Wrap = False
            .AutoGenerateColumns = False
            .EmptyDataText = "No Record Found."
            .DataSource = bizCust.GetCustomers()
        trRow = New HtmlTableRow
        tdCell = New HtmlTableCell
        tdCell.Align = "Left"
    End Sub
    Private Sub CustomizeThisGrid(ByRef myGridView As GridView)
        Dim col0 As BoundField = New BoundField()
        Dim col1 As BoundField = New BoundField()
        Dim col2 As BoundField = New BoundField()
        col0.ItemStyle.HorizontalAlign = HorizontalAlign.Left
        col0.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
        col0.HeaderText = "ID#"
        col0.DataField = "CustID"
        col1.ItemStyle.HorizontalAlign = HorizontalAlign.Left
        col1.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
        col1.HeaderText = "Name"
        col1.DataField = "CustName"
        col2.ItemStyle.HorizontalAlign = HorizontalAlign.Left
        col2.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
        col2.HeaderText = "Address"
        col2.DataField = "CustAddress"
    End Sub
 End Class

Output at Run time

Comments (10) -

  • mike

    8/23/2007 8:53:29 AM | Reply

    very handy mate, the best example i have seen.
    Thanks very much

  • mike

    8/23/2007 9:41:17 AM | Reply

    one thing though, how can i add a edit,update,delete etc to this?

  • HN

    8/26/2007 3:54:34 PM | Reply

    Thanks for the example.  One question...How do you sort the dynamic gridview?

  • RD

    10/2/2007 2:43:37 PM | Reply

    this coding is very useful for me. thanks a lot...
    i expect more like this...

  • Kapil

    2/24/2008 11:47:00 AM | Reply

    hello!!!I want to create a grid view at run time...Plz Help Me.

  • Unni

    2/25/2008 7:05:36 AM | Reply

    could any one tell me where i can get this name space "Vishwa.Example.Business", other wise it is not possible to use this

  • Vishwa

    2/25/2008 12:59:46 PM | Reply

    This is a simple assembly containing Customer Business Object, the code is available at However, this is just an example, you can use your own datasource or business object to do the same.

  • Biju

    5/20/2008 5:44:41 AM | Reply

      Can you tell me how to add textBox into the gridview column at runtime?

  • Jitender

    10/2/2008 9:32:49 AM | Reply

    the upper given example is good but i m having a query if we want to do one thing is that when ever we click on any row then according to row clicking event it will show the first column value in a label kindly send me the solution as soon as possible

  • Lyn

    8/20/2009 8:52:39 AM | Reply

    Great post - keep it up man Smile