Dynamic ButtonColumn in datagrid?

Discussion in 'ASP .Net Datagrid Control' started by Richard, Apr 13, 2004.

  1. Richard

    Richard Guest

    Hi all

    I've been looking at dynamicallly adding a ButtonColumn in a datagrid
    by implementing ITemplate. The datagrid has been designed as a
    usercontrol. I don't yet have the skill to know how to deal with the
    events so that an event can been seen on the hosting page. I've
    scoured the net for examples of how to do this but there doesn't seem
    to be much demand for it. Does anyone know of an example I could
    follow, or know how to do it?

    Any help much appreciated.

    Thanks
    Richard


    For what it's worth, here is what I've done so far, which is to get
    the column to show:

    'method in usercontrol called by hosting page in Page_Load event
    Public Sub BoundButtonColumnAdd()
    Dim bcb As New TemplateColumn
    bcb.ItemTemplate = New ButtonTemplate("col")
    DataGrid1.Columns.Add(bcb)
    End Sub

    Public Class ButtonTemplate
    Implements ITemplate
    Private mColName As String

    Public Sub New(ByVal ColName As String)
    mColName = ColName
    End Sub

    Public Overridable Overloads Sub InstantiateIn(ByVal Container As
    Control) Implements ITemplate.InstantiateIn

    Dim oButton As Button = New Button
    oButton.ID = mColName.Replace(" ", "")
    oButton.Text = mColName

    AddHandler oButton.DataBinding, AddressOf BindControl
    AddHandler oButton.Click, AddressOf ButtonClick

    Container.Controls.Add(oButton)

    End Sub

    Public Sub BindControl(ByVal Sender As Object, ByVal E As
    EventArgs)

    Dim oButton As Button = CType(Sender, Button)
    Dim oContainer As DataGridItem =
    CType(oButton.NamingContainer,
    DataGridItem)
    'setting the button text to a particular column value
    '(this is uncomplete but is roughly what I want to do)
    If oContainer.DataItem(mColName).GetType.ToString =
    "System.DBNull"
    Then
    oButton.Text = ""
    Else
    oButton.Text = oContainer.DataItem(mColName)
    End If
    End Sub

    Public Sub ButtonClick(ByVal Sender As Object, ByVal E As
    EventArgs)

    'seems to me that I should raise a click event here

    End Sub
    End Class
     
    Richard, Apr 13, 2004
    #1
    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. Northern
    Replies:
    0
    Views:
    412
    Northern
    Jul 22, 2003
  2. CGuy
    Replies:
    2
    Views:
    2,032
  3. John
    Replies:
    3
    Views:
    3,257
    Giorgio Parmeggiani
    Nov 3, 2003
  4. RSB
    Replies:
    6
    Views:
    7,730
    Karim
    May 19, 2004
  5. zigjog
    Replies:
    0
    Views:
    181
    zigjog
    Sep 29, 2003
Loading...

Share This Page