Home All Groups Group Topic Archive Search About

Adding a Directive to a dynamically created Template Column on gri

Author
28 Dec 2005 7:56 PM
Angel
I want to add a directive to a Template Column that I created at runtime
using the following code:
Sub CreateColumn()
            Dim tc1 As New TemplateColumn
            tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header,
"TEST")
            tc1.ItemStyle.CssClass = "datagridcell"
            tc1.ItemStyle.Width = New Unit("100%")
            tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "TEST")
            grid.columns.add(tc1)


End Sub

    Private Class DataGridTemplate
        Implements ITemplate

        Dim templateType As ListItemType
        Dim columnName As String

        Sub New(ByVal type As ListItemType, ByVal ColName As String)
            templateType = type
            columnName = ColName
        End Sub

        Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn

            Dim lc As New Literal
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = "<B><NOBR>" & columnName & "</NOBR></B>"
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    'container.Controls.Add(lc)
                    Dim lb As New System.Web.UI.WebControls.Label
                    lb.ID = "lbl" & columnName.Trim
                    lb.Text = "<%#
GetBucketCount(Container.DataItem(''strCount''), 0)%>"
                    container.Controls.Add(lb)
            End Select

        End Sub

    End Class


As you can see from the code I want to add the following directive:

<%# GetBucketCount(Container.DataItem(''strCount''), 0)%>

So when the WebGrid is bound with its data(grid.dataBind) it formats the
column correctly. The problem that I am having with the code it does not fill
in the data called by the directive. Does anyone has any thoughts on how to
set a directive to a dynamically created web grid column.

thanks in advance.

Author
28 Dec 2005 9:40 PM
Phillip Williams
Since you have your datagrid TemplateColumn dynamically defined, you should
add one more method in it to handle the databinding so that you can format
the data to your satisfaction. For example within the InstantiateIn method
you can add:

    AddHandler lbl.DataBinding, AddressOf BucketCount_DataBinding

and then you would add the handling method:

Private Sub BucketCount_DataBinding(ByVal sender As Object, _
            ByVal e As System.EventArgs)
    'get a reference to the datagriditem which is usually a datarowview
unless you have
    'a customized Business Logic Layer's list instead of the datatable bound
to the datagrid
    Dim container As DataGridItem
    Dim drv As DataRowView = CType(CType(sender.NamingContainer,
DataGridItem).DataItem, DataRowView)

   'verify that this is the method handling the databinding for the
   'label that will hold the bucketcount

    If TypeOf sender Is Label Then
    'cast the sender objec to its appropriate type
    Dim lbl As Label = CType(sender, Label)
    Dim strcount As String = drv("strCount")
    'add any code here to format the field value
                'before you assign it to the label text
    lbl.Text = drv("strCount")

    End If
End Sub




Show quoteHide quote
"Angel" wrote:

> I want to add a directive to a Template Column that I created at runtime
> using the following code:
> Sub CreateColumn()
>             Dim tc1 As New TemplateColumn
>             tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header,
> "TEST")
>             tc1.ItemStyle.CssClass = "datagridcell"
>             tc1.ItemStyle.Width = New Unit("100%")
>             tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, "TEST")
>             grid.columns.add(tc1)
>
>
> End Sub
>
>     Private Class DataGridTemplate
>         Implements ITemplate
>
>         Dim templateType As ListItemType
>         Dim columnName As String
>
>         Sub New(ByVal type As ListItemType, ByVal ColName As String)
>             templateType = type
>             columnName = ColName
>         End Sub
>
>         Sub InstantiateIn(ByVal container As Control) Implements
> ITemplate.InstantiateIn
>
>             Dim lc As New Literal
>             Select Case templateType
>                 Case ListItemType.Header
>                     lc.Text = "<B><NOBR>" & columnName & "</NOBR></B>"
>                     container.Controls.Add(lc)
>                 Case ListItemType.Item
>                     'container.Controls.Add(lc)
>                     Dim lb As New System.Web.UI.WebControls.Label
>                     lb.ID = "lbl" & columnName.Trim
>                     lb.Text = "<%#
> GetBucketCount(Container.DataItem(''strCount''), 0)%>"
>                     container.Controls.Add(lb)
>             End Select
>
>         End Sub
>
>     End Class
>
>
> As you can see from the code I want to add the following directive:
>
> <%# GetBucketCount(Container.DataItem(''strCount''), 0)%>
>
> So when the WebGrid is bound with its data(grid.dataBind) it formats the
> column correctly. The problem that I am having with the code it does not fill
> in the data called by the directive. Does anyone has any thoughts on how to
> set a directive to a dynamically created web grid column.
>
> thanks in advance.