J
jonefer
Ok, I found a good article of how to dynamically build template columns
The reason I want to use this is so that I can only set certain columns to
be sortable.
what I'm trying to do is read the value of a column - for example if the
column reads 'General Course' in the first 14 letters... I want that field to
be sortable
So here is the call that actually builds and populates my gridview - I just
wonder why I can't change any properties in the template?
lCount = tb_ActionList.GetActionList(sSL, sDIR, sSup, sLocName,
sDeptName, "Gen").Tables("qActionGrid_GEN").Rows.Count
If lCount = 0 Then
ShowGrid(False)
Me.lblCheck.Text = "No records match the selected criteria"
With Me.lblCheck
.ForeColor = Drawing.Color.Red
End With
Else
'try dynamic adding of template columns
For Each col As DataColumn In ds.Tables("qActionGrid_Gen").Columns
Dim bfield As TemplateField = New TemplateField
bfield.HeaderTemplate = New
GridViewTemplate(ListItemType.Header, col.ColumnName)
bfield.ItemTemplate = New
GridViewTemplate(ListItemType.Item, col.ColumnName)
Dim colchk As String = Left(col.ColumnName, 14)
'If colchk = "General Course" Then
bfield.SortExpression = col.ColumnName
'End If
'bfield.ItemStyle.Font.Names
gvActionList.Columns.Add(bfield)
Next
'=======================================
gvActionList.DataSource = ds
gvActionList.DataBind()
Here is the class
Public Class GridViewTemplate
Implements ITemplate
Private _templateType As ListItemType
Private _columnName As String
Public Sub New(ByVal type As ListItemType, ByVal colname As String)
_templateType = type
_columnName = colname
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements ITemplate.InstantiateIn
Select Case _templateType
Case ListItemType.Header
Dim lbl As Label = New Label
lbl.Text = _columnName
container.Controls.Add(lbl)
' break
Case ListItemType.Item
Dim tb1 As TextBox = New TextBox
AddHandler tb1.DataBinding, AddressOf tb1_DataBinding
tb1.Columns = 4
container.Controls.Add(tb1)
' break
Case ListItemType.EditItem
' break
Case ListItemType.Footer
Dim chkColumn As CheckBox = New CheckBox
chkColumn.ID = "Chk" + _columnName
container.Controls.Add(chkColumn)
' break
End Select
End Sub
Sub tb1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim txtdata As TextBox = CType(sender, TextBox)
Dim container As GridViewRow = CType(txtdata.NamingContainer,
GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem,
_columnName)
If Not (dataValue Is DBNull.Value) Then
txtdata.Text = dataValue.ToString
End If
End Sub
End Class
I feel I am so close to solving this... if I can just get these particular
columns to be searchable it would help me tremendously.
The reason I want to use this is so that I can only set certain columns to
be sortable.
what I'm trying to do is read the value of a column - for example if the
column reads 'General Course' in the first 14 letters... I want that field to
be sortable
So here is the call that actually builds and populates my gridview - I just
wonder why I can't change any properties in the template?
lCount = tb_ActionList.GetActionList(sSL, sDIR, sSup, sLocName,
sDeptName, "Gen").Tables("qActionGrid_GEN").Rows.Count
If lCount = 0 Then
ShowGrid(False)
Me.lblCheck.Text = "No records match the selected criteria"
With Me.lblCheck
.ForeColor = Drawing.Color.Red
End With
Else
'try dynamic adding of template columns
For Each col As DataColumn In ds.Tables("qActionGrid_Gen").Columns
Dim bfield As TemplateField = New TemplateField
bfield.HeaderTemplate = New
GridViewTemplate(ListItemType.Header, col.ColumnName)
bfield.ItemTemplate = New
GridViewTemplate(ListItemType.Item, col.ColumnName)
Dim colchk As String = Left(col.ColumnName, 14)
'If colchk = "General Course" Then
bfield.SortExpression = col.ColumnName
'End If
'bfield.ItemStyle.Font.Names
gvActionList.Columns.Add(bfield)
Next
'=======================================
gvActionList.DataSource = ds
gvActionList.DataBind()
Here is the class
Public Class GridViewTemplate
Implements ITemplate
Private _templateType As ListItemType
Private _columnName As String
Public Sub New(ByVal type As ListItemType, ByVal colname As String)
_templateType = type
_columnName = colname
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements ITemplate.InstantiateIn
Select Case _templateType
Case ListItemType.Header
Dim lbl As Label = New Label
lbl.Text = _columnName
container.Controls.Add(lbl)
' break
Case ListItemType.Item
Dim tb1 As TextBox = New TextBox
AddHandler tb1.DataBinding, AddressOf tb1_DataBinding
tb1.Columns = 4
container.Controls.Add(tb1)
' break
Case ListItemType.EditItem
' break
Case ListItemType.Footer
Dim chkColumn As CheckBox = New CheckBox
chkColumn.ID = "Chk" + _columnName
container.Controls.Add(chkColumn)
' break
End Select
End Sub
Sub tb1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim txtdata As TextBox = CType(sender, TextBox)
Dim container As GridViewRow = CType(txtdata.NamingContainer,
GridViewRow)
Dim dataValue As Object = DataBinder.Eval(container.DataItem,
_columnName)
If Not (dataValue Is DBNull.Value) Then
txtdata.Text = dataValue.ToString
End If
End Sub
End Class
I feel I am so close to solving this... if I can just get these particular
columns to be searchable it would help me tremendously.