DropDownList in cell in DataGrid

S

S?ren Olesen

Hi

I'm trying to dynamically populate a datagrid with cells/columns. Some
of the columns should contain cells with a DropDown list. I've managed
to create my grid (see code below), however I can't figure out how to
control the selectedvalue in the DropDownList.
I've tried to set the selected value during the OnBindData event,
however it doesn't seem to work.....

Ant help would be greatly appreciated.....

Public Class WebForm1
Inherits System.Web.UI.Page


Private Class DataGridTemplate
Implements ITemplate
Dim dv As DataView

Sub New(Optional ByRef mydata As DataView = Nothing)
dv = mydata
End Sub


Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
Dim lc As New Literal
If Not IsNothing(dv) Then
Dim dl As New DropDownList
dl.AutoPostBack = False
AddHandler dl.DataBinding, AddressOf Me.OnBindData
container.Controls.Add(dl)
dl.DataTextField = "Attribute"
dl.DataSource = dv
dl.DataBind()
container.Controls.Add(dl)
End If
End Sub
Sub OnBindData(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim dd As DropDownList = CType(sender, DropDownList)
Dim container As DataGridItem
If Not IsNothing(dd.NamingContainer) Then
container = CType(dd.NamingContainer, DataGridItem)
If Not IsNothing(container) Then
Dim cellvalue As String =
DataBinder.Eval(container.DataItem, "Column1")
dd.SelectedValue = cellvalue
End If
End If
End Sub

End Class

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents DataGrid1 As
System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the
Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim mylist As ListBox

DataGrid1.AutoGenerateColumns = False

Dim dropdowndata As DataTable = New DataTable
dropdowndata.Columns.Add(New DataColumn("Attribute",
GetType(String)))
For n As Integer = 0 To 9
Dim dr As DataRow = dropdowndata.NewRow()
dr(0) = "Value" & n.ToString
dropdowndata.Rows.Add(dr)
Next
Dim dv As DataView = New DataView(dropdowndata)

Dim tc1 As New TemplateColumn
tc1.ItemTemplate = New DataGridTemplate(dv)
DataGrid1.Columns.Add(tc1)

Dim tc2 As New BoundColumn
tc2.HeaderText = "Column2"
tc2.DataField = "Column2"
DataGrid1.Columns.Add(tc2)


Dim gridtata As DataTable = New DataTable
gridtata.Columns.Add(New DataColumn("Column1",
GetType(String)))
gridtata.Columns.Add(New DataColumn("Column2",
GetType(String)))
Dim dr2 As DataRow = gridtata.NewRow()
dr2(0) = "Value5"
dr2(1) = "Value 1,2"
gridtata.Rows.Add(dr2)
Dim dr3 As DataRow = gridtata.NewRow()
dr3(0) = "Value9"
dr3(1) = "Value 2,2"
gridtata.Rows.Add(dr3)
Dim dv1 As DataView = New DataView(gridtata)


DataGrid1.DataSource = dv1
DataGrid1.DataBind()

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