65.9K
CodeProject is changing. Read more.
Home

Custom GridView

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (3 votes)

Sep 27, 2008

CPOL

2 min read

viewsIcon

33311

downloadIcon

348

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

<%@ 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.

    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

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