S
Søren M. Olesen
Hi
(Sorry if this has meen answered a million times before...I just haven't
been able to find the answer)
I'm trying to dynamically create a datagrid containing a cell as a dropdown.
I seem to get both the datagrid and the dropdown populated (see code below)
with the correct values, however I haven't yet been able to get the selected
value in the dropdown to match the value of the cell behind.
It seems like I have to set the selected value in the OnDataLoad event,
however I'm not able to get the value from the cell behind in
OnDataLoad...so what't the trix ??
TIA
Søren
Public Class WebForm1
Inherits System.Web.UI.Page
#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
Public Class myDDTemplate
Implements ITemplate
Private lov As ArrayList
Private attribute As String
Sub New(ByVal l As ArrayList, ByVal a As String)
lov = l
Attribute = a
End Sub
Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
If Not IsNothing(lov) Then
Dim dl As New DropDownList
dl.AutoPostBack = False
AddHandler dl.DataBinding, AddressOf Me.OnBindData
AddHandler dl.Load, AddressOf Me.OnDataLoad
dl.ID = attribute
dl.DataSource = lov
dl.DataBind()
container.Controls.Add(dl)
End If
End Sub
Sub OnDataLoad(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 value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
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 value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
End If
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim attribute As String = "DXP"
Dim lov As ArrayList = New ArrayList
For i As Integer = 0 To 5
lov.Add("Item" & i)
Next
Dim dt As DataTable = New DataTable
dt.Columns.Add(New DataColumn(attribute, GetType(String)))
Dim dr As DataRow = dt.NewRow()
dr(0) = "Item5"
dt.Rows.Add(dr)
Dim dv As DataView = New DataView(dt)
DataGrid1.AutoGenerateColumns = False
Dim tc1 As New TemplateColumn
tc1.ItemTemplate = New myDDTemplate(lov, attribute)
tc1.HeaderText = attribute
DataGrid1.Columns.Add(tc1)
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
End Class
(Sorry if this has meen answered a million times before...I just haven't
been able to find the answer)
I'm trying to dynamically create a datagrid containing a cell as a dropdown.
I seem to get both the datagrid and the dropdown populated (see code below)
with the correct values, however I haven't yet been able to get the selected
value in the dropdown to match the value of the cell behind.
It seems like I have to set the selected value in the OnDataLoad event,
however I'm not able to get the value from the cell behind in
OnDataLoad...so what't the trix ??
TIA
Søren
Public Class WebForm1
Inherits System.Web.UI.Page
#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
Public Class myDDTemplate
Implements ITemplate
Private lov As ArrayList
Private attribute As String
Sub New(ByVal l As ArrayList, ByVal a As String)
lov = l
Attribute = a
End Sub
Sub InstantiateIn(ByVal container As Control) Implements
ITemplate.InstantiateIn
If Not IsNothing(lov) Then
Dim dl As New DropDownList
dl.AutoPostBack = False
AddHandler dl.DataBinding, AddressOf Me.OnBindData
AddHandler dl.Load, AddressOf Me.OnDataLoad
dl.ID = attribute
dl.DataSource = lov
dl.DataBind()
container.Controls.Add(dl)
End If
End Sub
Sub OnDataLoad(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 value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
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 value As String = CStr(DataBinder.Eval(container.DataItem, attribute))
End If
End If
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim attribute As String = "DXP"
Dim lov As ArrayList = New ArrayList
For i As Integer = 0 To 5
lov.Add("Item" & i)
Next
Dim dt As DataTable = New DataTable
dt.Columns.Add(New DataColumn(attribute, GetType(String)))
Dim dr As DataRow = dt.NewRow()
dr(0) = "Item5"
dt.Rows.Add(dr)
Dim dv As DataView = New DataView(dt)
DataGrid1.AutoGenerateColumns = False
Dim tc1 As New TemplateColumn
tc1.ItemTemplate = New myDDTemplate(lov, attribute)
tc1.HeaderText = attribute
DataGrid1.Columns.Add(tc1)
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End Sub
End Class