Dynamic Gridview Generation at run time

by Vishwa 7. February 2007 17:28

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  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 : Vishwa@VishwaMohan.com
 
''' 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.Controls.Add(lblTitleText)
 
        tdCell.Height = "30px"
 
        trRow.Cells.Add(tdCell)
 
        tblMain.Rows.Add(trRow)
 
 
 
        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."
 
            CustomizeThisGrid(gvwGridView)
 
            .DataSource = bizCust.GetCustomers()
 
            .DataBind()
 
        EndWith
 
 
 
        trRow = New HtmlTableRow
 
        tdCell = New HtmlTableCell
 
        tdCell.Align = "Left"
 
        tdCell.Controls.Add(gvwGridView)
 
        trRow.Cells.Add(tdCell)
 
        tblMain.Rows.Add(trRow)
 
        plhCustomer.Controls.Add(tblMain)
 
 
 
    End Sub
 
 
 
    Private Sub CustomizeThisGrid(ByRef myGridView As GridView)
 
        myGridView.Columns.Clear()
 
        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"
 
        myGridView.Columns.Add(col0)
 
 
 
        col1.ItemStyle.HorizontalAlign = HorizontalAlign.Left
 
        col1.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
 
        col1.HeaderText = "Name"
 
        col1.DataField = "CustName"
 
        myGridView.Columns.Add(col1)
 
 
 
        col2.ItemStyle.HorizontalAlign = HorizontalAlign.Left
 
        col2.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
 
        col2.HeaderText = "Address"
 
        col2.DataField = "CustAddress"
 
 
 
        myGridView.Columns.Add(col2)
 
 
 
    End Sub
 
 End Class
 

Output at Run time

Tags:

.NET

Comments (10) -

mike
mike United Kingdom
8/23/2007 8:53:29 AM #

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

mike
mike United Kingdom
8/23/2007 9:41:17 AM #

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

HN
HN United States
8/26/2007 3:54:34 PM #

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

RD
RD India
10/2/2007 2:43:37 PM #

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

Kapil
Kapil India
2/24/2008 11:47:00 AM #

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

Unni
Unni India
2/25/2008 7:05:36 AM #

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
Vishwa United States
2/25/2008 12:59:46 PM #

This is a simple assembly containing Customer Business Object, the code is available at www.vishwamohan.com/ShowArticle.aspx?ArticleID=41. However, this is just an example, you can use your own datasource or business object to do the same.

Biju
Biju India
5/20/2008 5:44:41 AM #

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

Jitender
Jitender India
10/2/2008 9:32:49 AM #

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
Lyn Denmark
8/20/2009 8:52:39 AM #

Great post - keep it up man Smile

Comments are closed

About Me

Me Hello,my name is
Vishwa Mohan Kumar, PMP
MCP SOA Certified Architect
I am a Software Architect. This blog is result of my experiments.

Flickr Photos

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Archive

Recent Comments

Comment RSS

Live Traffic Feed