Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET

Custom GridView

Rate me:
Please Sign up or sign in to vote.
1.00/5 (3 votes)
26 Sep 2008CPOL2 min read 33.1K   348   17  
Configurable Gridview using file or database

Introduction

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.

Description

HTML section of the user control "ucCustomDataGrid.ascx" looks like

VB.NET
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ucCustomDataGrid.ascx.vb" 
  Inherits="ucCustomDataGrid" %> <asp:GridView ID="grdvwGeneric" runat="server"> 
<table>
<tbody>
<tr>
  <td colspan="1"><asp:GridView ID="grdvwGeneric" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></td>
</tr>
<tr id="trButtonRow" runat="server"/>
<tr>
  <td colspan="1"><asp:Button ID="btnDelete" runat="server" Text="Delete" /></td>
  <td colspan="1"><asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
</tr>
</tbody>
</table>

User Control VB code : ucCustomDataGrid.ascx.vb


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.

VB.NET
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.

VB.NET
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.

VB.NET
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

VB.NET
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

Background

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.

Using the code

The code is developed in Visual Studio 2005 using ASP 2.0 and VB.NET. This Project contains the following files

  1. Under DataFile Folder, there are two files
    1. Book.xml
    2. 2) Configuration.xml
  2. Web Page
    Default.aspx, Default.aspx.vb
  3. Web User Control
    ucCustomDataGrid.ascx ucCustomDataGrid.ascx.vb

How to execute the project

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

Codeproject_Image.GIF

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Siliconguys Inc
United States United States
Worked in various projects inlcuding WPF, WCF, Silverlight, MongoDB, Hadoop and Web development projects using ASP.NET, AJAX, C#, JavaScript, Web Services and SQL Server and Oracle.

Comments and Discussions

 
-- There are no messages in this forum --