Dynamic ButtonColumn in datagrid?

R

Richard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top