Wednesday, February 07, 2007

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


 

No comments: