![]() |
Web Development »
ASP.NET Controls »
Grid Controls
Intermediate
License: The Code Project Open License (CPOL)
Custom GridViewBy Rajibdotnet05Configurable Gridview using file or database |
VB (VB 7.x), .NET, ASP.NET, Architect, Design
|
||||||||
|
Advanced Search |
|
|
|
||||||||||||||||
This project is about a user control using GridView which can be configured using Database or File. Basically, usercontrol knows nothing about the data until the container page (here Default.aspx) sends two datasets. The User Control comprises of a GridView, a Textbox and two buttons.
HTML section of the user control "ucCustomDataGrid.ascx" looks like
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ucCustomDataGrid.ascx.vb" Inherits="ucCustomDataGrid" %>
<tr id="trButtonRow" runat="server">
<asp:GridView ID="grdvwGeneric" runat="server">
<asp:TextBox ID="TextBox1" runat="server">
align="center" colspan="1">
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
align="center" colspan="1">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
Everything that a GridView needs, has to be defined by property inside the user control.
Following code shows the two datasets that are most important for the user control.
Private dsCustom As DataSet
Private dsCONFIG As DataSet
Public WriteOnly Property SetDataSource() As DataSet
Set(ByVal value As DataSet)
dsCustom = value
End Set
End Property
Public WriteOnly Property SetConfigSource() As DataSet
Set(ByVal value As DataSet)
dsCONFIG = value
End Set
End Property
Further, inside the Page Load section of User Control, following datasets will be used.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ViewState("DataSet") = dsCustom
ViewState("ConfigSet") = dsCONFIG
Else
dsCustom = ViewState("DataSet")
dsCONFIG = ViewState("ConfigSet")
End If
BindDataGrid()
End Sub
Other important function worth mentioning here is RowBound which will configure the rows and columns dynamically.
Protected Sub grdvwGeneric_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdvwGeneric.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then GetCustomDataGridColumn(e) End If End Sub Private Sub GetCustomDataGridColumn(ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) //Read from the Configuration DataSet Dim btnCustom As New Control For iType As Integer = 0 To dsCONFIG.Tables(0).Rows.Count - 1 If dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "LinkButton" Then Dim btn As New LinkButton btn.Text = e.Row.Cells(0).Text btn.CommandName = e.Row.Cells(1).Text btn.CommandArgument = e.Row.Cells(1).Text AddHandler btn.Command, AddressOf Checkin_Command btnCustom = btn ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "Button" Then Dim btn1 As New Button btn1.Text = e.Row.Cells(1).Text btn1.CommandName = e.Row.Cells(1).Text btn1.CommandArgument = e.Row.Cells(1).Text AddHandler btn1.Command, AddressOf Checkin_Command btnCustom = btn1 ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "DropDownList" Then Dim btn2 As New DropDownList btn2.Text = e.Row.Cells(2).Text btn2.Items.Add("Select") btn2.Items.Add(e.Row.Cells(2).Text) btn2.AutoPostBack = True AddHandler btn2.SelectedIndexChanged, AddressOf Dropdown btnCustom = btn2 ElseIf dsCONFIG.Tables(0).Rows(iType)("ColumnType") = "CheckBox" Then Dim btn3 As New CheckBox AddHandler btn3.CheckedChanged, AddressOf Check_Changed btnCustom = btn3 End If e.Row.Cells(iType).Controls.Add(btnCustom) Next End Sub
Apart from that, the Default.aspx will have the following code to set the properties for the user control
Dim dsCustom As DataSet
Dim dsCONFIG As DataSet
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
GetDataSet(dsCustom, Server.MapPath("~/DataFile/Book.xml"))
GetDataSet(dsCONFIG, Server.MapPath("~/DataFile/Configuration.xml"))
//This file is the container or test file, which does the following task.
//Passes the configuration and data to the user control.
UcCustomDataGrid1.SetDataSource = dsCustom
UcCustomDataGrid1.SetConfigSource = dsCONFIG
UcCustomDataGrid1.SetGridStyle = ucCustomDataGrid.NamedStyle.AUTUMN
End If
End Sub
//This subroutine reads the xml file and bind it to the dataset
Private Sub GetDataSet(ByRef ds As DataSet, ByVal filename As String)
ds = New DataSet
Dim xmlMenuDoc As XmlDocument = New XmlDocument()
xmlMenuDoc.Load(filename)
Dim xmlString As StringReader = New StringReader(xmlMenuDoc.InnerXml)
ds.ReadXml(xmlString)
End Sub //Get the relevant XML file and populate a DataSet
I am working for a project which uses a lot of GridView. Every GridView has different functionality. For example, few are view only, few have checkboxes only, and few are sortable, few needs paging facility and so on. Requirement was such that everything should be configurable (properties read from a file such as web.config) or database driven. A consumer of the GridView would know nothing about the GridView in advance, such as header, footer, column name, field name and so on; all would come from the database. Even the client side functionality inside the child control of the GridView would be driven by database.
The code is developed in Visual Studio 2005 using ASP 2.0 and VB.NET.
This Project contains the following files
A) Under DataFile Folder, there are two file 1) Book.xml 2) Configuration.xml B) Web Page Default.aspx, Default.aspx.vb C) Web User Control ucCustomDataGrid.ascx ucCustomDataGrid.ascx.vb
Set Default.aspx as the start page and simply run it from Visual Studio.Net or Create a Virtual Directory from IIS and run the Default.aspx
Here is how the output will appear once you execute the default.aspx
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 27 Sep 2008 Editor: |
Copyright 2008 by Rajibdotnet05 Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |