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
- <%@ 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>
Code Behind
Option Explicit OnOption Strict OnImports 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 CustomerDynamicGridViewInherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMe.LoadIfNot Page.IsPostBack ThenCall PopulateDynamicGrid()End SubPrivate 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.NavylblTitleText.ForeColor = Drawing.Color.WhitetdCell.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()EndWithtrRow = 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 SubPrivate 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.Leftcol0.HeaderStyle.HorizontalAlign = HorizontalAlign.Leftcol0.HeaderText = "ID#"
col0.DataField = "CustID"
myGridView.Columns.Add(col0)col1.ItemStyle.HorizontalAlign = HorizontalAlign.Leftcol1.HeaderStyle.HorizontalAlign = HorizontalAlign.Leftcol1.HeaderText = "Name"
col1.DataField = "CustName"
myGridView.Columns.Add(col1)col2.ItemStyle.HorizontalAlign = HorizontalAlign.Leftcol2.HeaderStyle.HorizontalAlign = HorizontalAlign.Leftcol2.HeaderText = "Address"
col2.DataField = "CustAddress"
myGridView.Columns.Add(col2)End SubEnd ClassOutput at Run time
No comments:
Post a Comment