METHOD WHERE THE TEMPLATE COLUMNS ARE ADDED DYNAMICALLY.
IT USES THE CLASS DataGridTemplete
Dim roles As New rolesDbSql
count = 0
oDataRows = roles.GETROW().Tables(0).Select
For Each oDataRow In oDataRows
If Not oDataRow("HeaderImageUrl").GetType.Name = "DBNull" And Not oDataRow("HeaderImageUrl").GetType.Name = "" Then
Dim dgc As TemplateColumn = New TemplateColumn
If oDataRow("roleid") = oDataRow("roletype") Then
dgc.ItemStyle.BackColor = Color.FromName("#4292C6")
dgc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgc.HeaderTemplate = New DataGridTemplete(ListItemType.Header, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
dgc.ItemTemplate = New DataGridTemplete(ListItemType.Item, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
UsersGrid.Columns.Add(dgc)
Else
dgc.ItemStyle.BackColor = Color.FromName("#DBEAF5")
dgc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgc.HeaderTemplate = New DataGridTemplete(ListItemType.Header, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
dgc.ItemTemplate = New DataGridTemplete(ListItemType.Item, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
UsersGrid.Columns.Add(dgc)//USERSGRID IS THE DATAGRID ID
End If
End If
count = count + 1
Next
End Sub
-----------------------------------------------------------------------------------------------------------
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
Namespace FileOn
'A customized class for displaying the Template Column
Public Class DataGridTemplete
Implements ITemplate
'A variable to hold the type of ListItemType.
Dim _templateType As ListItemType
'A variable to hold the column name.
Dim _columnName As String
'A variable to hold the url for the header image
Dim _Url As String
'Id for the checkbox
Dim chkId As String
'Constructor where we define the template type and column name.
Public Sub New(ByVal type As ListItemType, ByVal colname As String, ByVal url As String, ByVal id As String)
'Stores the template type.
_templateType = type
'Stores the column name.
_columnName = colname
_Url = url
chkId = id
End Sub
Private Sub ITemplate_InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
Select Case _templateType
Case ListItemType.Header
'Creates a new Image control and add it to the container.
Dim img As Image = New Image
'Alocate the image url
img.ImageUrl = _Url
'Assigns the image of the column in the lable.
container.Controls.Add(img)
'Adds the newly created label control to the container.
Exit Select
Case ListItemType.Item 'Creates a new text box control and add it to the container.
Dim chk As New CheckBox()
'Allocates the new text box object. AddHandler chk.DataBinding, AddressOf Me.chk_DataBinding
'Attaches the data binding event.
chk.ID = "chk" & chkId.ToString() chk.Enabled = False
chk.ToolTip = _columnName
container.Controls.Add(chk)
'Adds the newly created textbox to the container.
Exit Select
Case ListItemType.EditItem 'As, I am not using any EditItem, I didnot added any code here.
Exit Select
Case ListItemType.Footer Dim chkColumn As New CheckBox()
chkColumn.ID = "Chk" & _columnName
container.Controls.Add(chkColumn)
Exit Select
End Select
End Sub Private Sub chk_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = DirectCast(sender, CheckBox)
Dim container As DataGridItem = DirectCast(chk.NamingContainer, DataGridItem)
End Sub End ClassEnd Namespace
IT USES THE CLASS DataGridTemplete
Dim count As Integer
Dim oDataRows() As DataRow
Dim oDataRow As DataRow
Public Sub AddColumnToConfig()Dim oDataRows() As DataRow
Dim oDataRow As DataRow
Dim roles As New rolesDbSql
count = 0
oDataRows = roles.GETROW().Tables(0).Select
For Each oDataRow In oDataRows
If Not oDataRow("HeaderImageUrl").GetType.Name = "DBNull" And Not oDataRow("HeaderImageUrl").GetType.Name = "" Then
Dim dgc As TemplateColumn = New TemplateColumn
If oDataRow("roleid") = oDataRow("roletype") Then
dgc.ItemStyle.BackColor = Color.FromName("#4292C6")
dgc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgc.HeaderTemplate = New DataGridTemplete(ListItemType.Header, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
dgc.ItemTemplate = New DataGridTemplete(ListItemType.Item, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
UsersGrid.Columns.Add(dgc)
Else
dgc.ItemStyle.BackColor = Color.FromName("#DBEAF5")
dgc.ItemStyle.HorizontalAlign = HorizontalAlign.Center
dgc.HeaderTemplate = New DataGridTemplete(ListItemType.Header, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
dgc.ItemTemplate = New DataGridTemplete(ListItemType.Item, oDataRow("RoleDescription").ToString(), oDataRow("HeaderImageUrl").ToString(), oDataRow("rolename").ToString().Replace(" ", ""))
UsersGrid.Columns.Add(dgc)//USERSGRID IS THE DATAGRID ID
End If
End If
count = count + 1
Next
End Sub
-----------------------------------------------------------------------------------------------------------
CLASS DataGridTemplete
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
Namespace FileOn
'A customized class for displaying the Template Column
Public Class DataGridTemplete
Implements ITemplate
'A variable to hold the type of ListItemType.
Dim _templateType As ListItemType
'A variable to hold the column name.
Dim _columnName As String
'A variable to hold the url for the header image
Dim _Url As String
'Id for the checkbox
Dim chkId As String
'Constructor where we define the template type and column name.
Public Sub New(ByVal type As ListItemType, ByVal colname As String, ByVal url As String, ByVal id As String)
'Stores the template type.
_templateType = type
'Stores the column name.
_columnName = colname
_Url = url
chkId = id
End Sub
Private Sub ITemplate_InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
Select Case _templateType
Case ListItemType.Header
'Creates a new Image control and add it to the container.
Dim img As Image = New Image
'Alocate the image url
img.ImageUrl = _Url
'Assigns the image of the column in the lable.
container.Controls.Add(img)
'Adds the newly created label control to the container.
Exit Select
Case ListItemType.Item 'Creates a new text box control and add it to the container.
Dim chk As New CheckBox()
'Allocates the new text box object. AddHandler chk.DataBinding, AddressOf Me.chk_DataBinding
'Attaches the data binding event.
chk.ID = "chk" & chkId.ToString() chk.Enabled = False
chk.ToolTip = _columnName
container.Controls.Add(chk)
'Adds the newly created textbox to the container.
Exit Select
Case ListItemType.EditItem 'As, I am not using any EditItem, I didnot added any code here.
Exit Select
Case ListItemType.Footer Dim chkColumn As New CheckBox()
chkColumn.ID = "Chk" & _columnName
container.Controls.Add(chkColumn)
Exit Select
End Select
End Sub Private Sub chk_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim chk As CheckBox = DirectCast(sender, CheckBox)
Dim container As DataGridItem = DirectCast(chk.NamingContainer, DataGridItem)
End Sub End ClassEnd Namespace