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

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CustomerDynamicGridView.aspx.vb" Inherits="CustomerDynamicGridView" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dynamic Grid View</title>
</head>
<body>
    <form id="frmCustomer" runat="server">
    <div style="text-align:center">
        <asp:PlaceHolder ID="plhCustomer" runat="server">
        </asp:PlaceHolder>    
    </div>   
    </form>
</body>
</html>

 

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 As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then Call PopulateDynamicGrid()

    End Sub

 

    Private Sub PopulateDynamicGrid()

        Dim gvwGridView As New GridView

        Dim tblMain As New HtmlTable

        Dim trRow As New HtmlTableRow

        Dim tdCell As New HtmlTableCell

        Dim lblTitleText As New Label

        Dim bizCust As New 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()

        End With

 

        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

 

 

Signature

Comments

8/23/2007 8:53:29 AM #

mike

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

mike United Kingdom

8/23/2007 9:41:17 AM #

mike

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

mike United Kingdom

8/26/2007 3:54:34 PM #

HN

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

HN United States

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

RD

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

RD India

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

Kapil

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

Kapil India

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

Unni

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

Unni India

2/25/2008 12:59:46 PM #

Vishwa

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.

Vishwa United States

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

Biju

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

Biju India

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

Jitender

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

Jitender India

8/20/2009 8:52:39 AM #

Lyn

Great post - keep it up man Smile

Lyn Denmark

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



About Me

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

Flickr Photos

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent Comments

Comment RSS

Live Traffic Feed