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

GridThemes

Rate me:
Please Sign up or sign in to vote.
4.92/5 (51 votes)
4 Mar 2007CPOL10 min read 168.7K   1.5K   161  
Combining a BuildProvider and IExtenderProvider to create a declarative framework for conditional formatting in ASP.NET GridViews.
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Public Class DataProvider

    'Create the sample datasource for these exercises
    'this simulates a stored procedure that returns variable value columns
    Public Shared Function CreateDataSource(ByVal numValueColumns As Integer) As DataTable
        Dim t As New DataTable()

        ' create the table structure
        Dim c As New DataColumn()

        c = New DataColumn()
        c.DataType = System.Type.GetType("System.String")
        c.ColumnName = "Category"
        t.Columns.Add(c)

        Dim i As Integer
        For i = 1 To numValueColumns
            c = New DataColumn()
            c.DataType = System.Type.GetType("System.Int32")
            c.ColumnName = "Value" + i.ToString()
            t.Columns.Add(c)
        Next

        ' populate the table with some sample rows of data
        Dim rnd As New Random()
        Dim r As DataRow = t.NewRow()
        r("Category") = "North Region"
        For i = 1 To numValueColumns
            r("Value" + i.ToString()) = rnd.Next(-10, 10)
        Next
        t.Rows.Add(r)

        r = t.NewRow()
        r("Category") = "South Region"
        For i = 1 To numValueColumns
            r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
        Next
        t.Rows.Add(r)

        r = t.NewRow()
        r("Category") = "East Region"
        For i = 1 To numValueColumns
            r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
        Next
        t.Rows.Add(r)

        r = t.NewRow()
        r("Category") = "West Region"
        For i = 1 To numValueColumns
            r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
        Next
        t.Rows.Add(r)

        Return t
    End Function


    Public Shared Function CreateCategoryDataSource(ByVal numValueColumns As Integer) As DataTable

        Dim t As New DataTable()

        ' create the table structure
        Dim c As New DataColumn()

        c = New DataColumn()
        c.DataType = System.Type.GetType("System.String")
        c.ColumnName = "Category"
        t.Columns.Add(c)

        Dim i As Integer
        For i = 1 To numValueColumns
            c = New DataColumn()
            c.DataType = System.Type.GetType("System.Int32")
            c.ColumnName = "Value" + i.ToString()
            t.Columns.Add(c)
        Next

        ' populate the table with some sample rows of data
        Dim rnd As New Random()
        Dim r As DataRow = t.NewRow()
        Dim top As Integer = rnd.Next(2, 8)
        Dim j As Integer

        For j = 0 To top
            r = t.NewRow()
            r("Category") = "North Region"
            For i = 1 To numValueColumns
                r("Value" + i.ToString()) = rnd.Next(-10, 10)
            Next
            t.Rows.Add(r)
        Next

        top = rnd.Next(2, 8)
        For j = 0 To top
            r = t.NewRow()
            r("Category") = "South Region"
            For i = 1 To numValueColumns
                r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
            Next
            t.Rows.Add(r)
        Next

        top = rnd.Next(2, 8)
        For j = 0 To top
            r = t.NewRow()
            r("Category") = "East Region"
            For i = 1 To numValueColumns
                r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
            Next
            t.Rows.Add(r)
        Next

        top = rnd.Next(2, 8)
        For j = 0 To top
            r = t.NewRow()
            r("Category") = "West Region"
            For i = 1 To numValueColumns
                r("Value" + i.ToString()) = rnd.Next(-10000, 10000)
            Next
            t.Rows.Add(r)
        Next

        Return t

    End Function





    Public Shared Function CreateCategoryWithSubGroupsDataSource(ByVal numValueColumns As Integer) As DataTable

        Dim t As New DataTable()

        ' create the table structure
        Dim c As New DataColumn()

        c = New DataColumn()
        c.DataType = System.Type.GetType("System.String")
        c.ColumnName = "Category"
        t.Columns.Add(c)

        c = New DataColumn()
        c.DataType = System.Type.GetType("System.String")
        c.ColumnName = "Subcategory"
        t.Columns.Add(c)

        Dim i As Integer
        For i = 1 To numValueColumns
            c = New DataColumn()
            c.DataType = System.Type.GetType("System.Int32")
            c.ColumnName = "Value" + i.ToString()
            t.Columns.Add(c)
        Next

        ' populate the table with some sample rows of data
        CreateRegionRows(t, "North Region", New String() {"Minnesota", "North Dakota"})
        CreateRegionRows(t, "South Region", New String() {"Louisiana", "Alabama", "Mississippi"})
        CreateRegionRows(t, "East Region", New String() {"New York", "Maine"})
        CreateRegionRows(t, "West Region", New String() {"California", "Nevada", "Oregon"})

        Return t
    End Function

    Private Shared Sub CreateRegionRows(ByVal t As DataTable, ByVal category As String, ByVal subCategories() As String)

        Dim rnd As Random = New Random()
        Dim r As DataRow
        Dim s As String
        Dim i As Integer
        Dim j As Integer
        Dim top As Integer

        For Each s In subCategories
            top = rnd.Next(1, 4)
            For j = 0 To top
                r = t.NewRow()
                r("Category") = category
                r("SubCategory") = s
                For i = 2 To t.Columns.Count - 1
                    r("Value" & (i - 1).ToString()) = rnd.Next(-100, 100)
                Next
                t.Rows.Add(r)
            Next
        Next

    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
University of Nevada, Las Vegas
United States United States
With a background in education, music, application development, institutional research, data governance, and business intelligence, I work for the University of Nevada, Las Vegas helping to derive useful information from institutional data. It's an old picture, but one of my favorites.

Comments and Discussions