Adding a Directive to a dynamically created Template Column on gri

Discussion in 'ASP .Net Web Controls' started by Angel, Dec 28, 2005.

  1. Angel

    Angel Guest

    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.
     
    Angel, Dec 28, 2005
    #1
    1. Advertising

  2. 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




    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "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.
     
    Phillip Williams, Dec 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gnewsgroup
    Replies:
    4
    Views:
    9,029
    bigal
    May 21, 2008
  2. msimmons
    Replies:
    0
    Views:
    548
    msimmons
    Jul 16, 2009
  3. johnsonlim026
    Replies:
    0
    Views:
    962
    johnsonlim026
    Feb 27, 2010
  4. MattB

    Column order and dynamically created columns

    MattB, Dec 1, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    146
    MattB
    Dec 1, 2004
  5. Ahmet Gunes
    Replies:
    0
    Views:
    195
    Ahmet Gunes
    Nov 7, 2003
Loading...

Share This Page