DropDownList in cell in DataGrid

Discussion in 'ASP .Net Web Controls' started by S?ren Olesen, Nov 27, 2003.

  1. S?ren Olesen

    S?ren Olesen Guest

    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
     
    S?ren Olesen, Nov 27, 2003
    #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. news.rcn.com
    Replies:
    1
    Views:
    9,680
    Chris Smith
    Jul 12, 2004
  2. =?Utf-8?B?Z2xlbm4=?=

    Two DropDownList controls in one DataGrid cell

    =?Utf-8?B?Z2xlbm4=?=, Apr 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,870
    =?Utf-8?B?Z2xlbm4=?=
    Apr 25, 2006
  3. Phillip Roncoroni
    Replies:
    14
    Views:
    1,100
    Toby A Inkster
    Apr 5, 2004
  4. Empire City
    Replies:
    3
    Views:
    414
    Empire City
    Apr 30, 2004
  5. RJ
    Replies:
    1
    Views:
    244
    Eliyahu Goldin
    Feb 17, 2005
Loading...

Share This Page