Databind dropdown in datagrid

Discussion in 'ASP .Net Datagrid Control' started by Søren M. Olesen, Feb 4, 2004.

  1. 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
     
    Søren M. Olesen, Feb 4, 2004
    #1
    1. Advertising

  2. Søren M. Olesen

    Earl Teigrob Guest

    Is this what you are looking for?

    ManagerId2.Items.FindByValue(pkManager).Selected=true;

    Where ManagerId2 is the dropdownlist

    "Søren M. Olesen" <> wrote in message
    news:%...
    > 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
    >
    >
    >
    >
    >
     
    Earl Teigrob, Feb 4, 2004
    #2
    1. Advertising

  3. No, not really.

    What I'm trying to do, is have a cell in a DataGrid as a DropDownList. I',
    populate the DataGrid with a DataView, and I'd like the selected value in
    the DropDown to match the value of the cell. However I simply can't figure
    out how to get the value from the cell, so I can set the selected value in
    the DropDownList

    Regards,

    Søren




    "Earl Teigrob" <> wrote in message
    news:%...
    > Is this what you are looking for?
    >
    > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    >
    > Where ManagerId2 is the dropdownlist
    >
    > "Søren M. Olesen" <> wrote in message
    > news:%...
    > > 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
    > >
    > >
    > >
    > >
    > >

    >
    >
     
    Søren M. Olesen, Feb 5, 2004
    #3
  4. Søren M. Olesen

    Earl Teigrob Guest

    In the ItemDataBound event, you can retrieve the currently bounding row of
    the view and get its values like this
    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

    if(e.Item.ItemType == ListItemType.EditItem)

    {

    DataRowView drv = (DataRowView) e.Item.DataItem;

    String currentgenre = drv["pkManager"].ToString();

    ....

    drv now had the current row form which you can retieve any data in that row.

    Hope that helps

    Earl



    "Søren M. Olesen" <> wrote in message
    news:uT01T$...
    > No, not really.
    >
    > What I'm trying to do, is have a cell in a DataGrid as a DropDownList. I',
    > populate the DataGrid with a DataView, and I'd like the selected value in
    > the DropDown to match the value of the cell. However I simply can't figure
    > out how to get the value from the cell, so I can set the selected value in
    > the DropDownList
    >
    > Regards,
    >
    > Søren
    >
    >
    >
    >
    > "Earl Teigrob" <> wrote in message
    > news:%...
    > > Is this what you are looking for?
    > >
    > > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    > >
    > > Where ManagerId2 is the dropdownlist
    > >
    > > "Søren M. Olesen" <> wrote in message
    > > news:%...
    > > > 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
    > > >
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Earl Teigrob, Feb 5, 2004
    #4
    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. yanky
    Replies:
    1
    Views:
    436
    yanky
    Jun 8, 2005
  2. wapsiii

    Dropdown databind to public enum

    wapsiii, Oct 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    5,008
    wapsiii
    Oct 22, 2005
  3. vishnu
    Replies:
    1
    Views:
    1,214
    Patrick.O.Ige
    Mar 25, 2006
  4. cmay
    Replies:
    2
    Views:
    1,316
  5. Michael
    Replies:
    0
    Views:
    226
    Michael
    Dec 29, 2003
Loading...

Share This Page