Datagrid with Drop Down Lists

Discussion in 'ASP .Net Datagrid Control' started by Robin, Aug 5, 2004.

  1. Robin

    Robin Guest

    In an asp.net page that has a datagrid, how do you change the data for that
    column to be listed in a listbox with the current value selected?
     
    Robin, Aug 5, 2004
    #1
    1. Advertising

  2. Check this page and click the link for The DropDownList Scenario:

    http://msdn.microsoft.com/library/d...y/en-us/dnaspp/html/creatingcustomcolumns.asp

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    Handles DataGrid1.ItemDataBound
    If e.Item.ItemType = ListItemType.EditItem Then
    Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
    Dim CurrentShip As String = DRV("ShipVia")
    Dim DDL As DropDownList = _
    CType(e.Item.Cells(4).Controls(1), DropDownList)
    Dim SQL As String = _
    "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
    Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
    Dim DS As New DataSet
    Dim item As ListItem
    DA.Fill(DS, "Shippers")
    DDL.DataSource = DS.Tables("Shippers").DefaultView
    DDL.DataTextField = "CompanyName"
    DDL.DataValueField = "ShipperID"
    DDL.DataBind()
    item = DDL.Items.FindByValue(CurrentShip)
    If Not item Is Nothing Then item.Selected = True
    End If
    End Sub


    "Robin" <> wrote in message
    news:...
    > In an asp.net page that has a datagrid, how do you change the data for
    > that
    > column to be listed in a listbox with the current value selected?
    >
    >
     
    Ken Cox [Microsoft MVP], Aug 6, 2004
    #2
    1. Advertising

  3. Robin

    Robin Guest

    When in mydatagrid_UpdateCommand

    Using myDataGrid.SelectedItem.Cells(0).Text or
    CType(DataGrid1.SelectedItem.Cells(0).Controls(0),

    DataBoundLiteralControl).Text

    Returns Referenced object has a value of 'Nothing'.

    Mydatagrid.selecteditem returns Nothing and Mydatagrid.selectedindex = -1

    What does the reference item need to be changed to?

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:OL%...
    > Check this page and click the link for The DropDownList Scenario:
    >
    >

    http://msdn.microsoft.com/library/d...y/en-us/dnaspp/html/creatingcustomcolumns.asp
    >
    > Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    > ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    > Handles DataGrid1.ItemDataBound
    > If e.Item.ItemType = ListItemType.EditItem Then
    > Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
    > Dim CurrentShip As String = DRV("ShipVia")
    > Dim DDL As DropDownList = _
    > CType(e.Item.Cells(4).Controls(1), DropDownList)
    > Dim SQL As String = _
    > "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
    > Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
    > Dim DS As New DataSet
    > Dim item As ListItem
    > DA.Fill(DS, "Shippers")
    > DDL.DataSource = DS.Tables("Shippers").DefaultView
    > DDL.DataTextField = "CompanyName"
    > DDL.DataValueField = "ShipperID"
    > DDL.DataBind()
    > item = DDL.Items.FindByValue(CurrentShip)
    > If Not item Is Nothing Then item.Selected = True
    > End If
    > End Sub
    >
    >
    > "Robin" <> wrote in message
    > news:...
    > > In an asp.net page that has a datagrid, how do you change the data for
    > > that
    > > column to be listed in a listbox with the current value selected?
    > >
    > >

    >
     
    Robin, Aug 9, 2004
    #3
  4. You best bet is to set trace="true" in your .aspx page so you can see the
    control hierarchy and count the objects. I suspect that you aren't
    referencing what you think in DataGrid1.SelectedItem.Cells(0).Controls(0).


    "Robin" <> wrote in message
    news:...
    >
    > When in mydatagrid_UpdateCommand
    >
    > Using myDataGrid.SelectedItem.Cells(0).Text or
    > CType(DataGrid1.SelectedItem.Cells(0).Controls(0),
    >
    > DataBoundLiteralControl).Text
    >
    > Returns Referenced object has a value of 'Nothing'.
    >
    > Mydatagrid.selecteditem returns Nothing and Mydatagrid.selectedindex = -1
    >
    > What does the reference item need to be changed to?
    >
    > "Ken Cox [Microsoft MVP]" <> wrote in message
    > news:OL%...
    >> Check this page and click the link for The DropDownList Scenario:
    >>
    >>

    > http://msdn.microsoft.com/library/d...y/en-us/dnaspp/html/creatingcustomcolumns.asp
    >>
    >> Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    >> ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    >> Handles DataGrid1.ItemDataBound
    >> If e.Item.ItemType = ListItemType.EditItem Then
    >> Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
    >> Dim CurrentShip As String = DRV("ShipVia")
    >> Dim DDL As DropDownList = _
    >> CType(e.Item.Cells(4).Controls(1), DropDownList)
    >> Dim SQL As String = _
    >> "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
    >> Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
    >> Dim DS As New DataSet
    >> Dim item As ListItem
    >> DA.Fill(DS, "Shippers")
    >> DDL.DataSource = DS.Tables("Shippers").DefaultView
    >> DDL.DataTextField = "CompanyName"
    >> DDL.DataValueField = "ShipperID"
    >> DDL.DataBind()
    >> item = DDL.Items.FindByValue(CurrentShip)
    >> If Not item Is Nothing Then item.Selected = True
    >> End If
    >> End Sub
    >>
    >>
    >> "Robin" <> wrote in message
    >> news:...
    >> > In an asp.net page that has a datagrid, how do you change the data for
    >> > that
    >> > column to be listed in a listbox with the current value selected?
    >> >
    >> >

    >>

    >
    >
     
    Ken Cox [Microsoft MVP], Aug 10, 2004
    #4
  5. Robin

    Rick Spiewak Guest

    And, why not use templates so you can just .FindControl by ID instead of
    trying to count cells and controls.

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:%...
    > You best bet is to set trace="true" in your .aspx page so you can see the
    > control hierarchy and count the objects. I suspect that you aren't
    > referencing what you think in DataGrid1.SelectedItem.Cells(0).Controls(0).
    >
    >
    > "Robin" <> wrote in message
    > news:...
    > >
    > > When in mydatagrid_UpdateCommand
    > >
    > > Using myDataGrid.SelectedItem.Cells(0).Text or
    > > CType(DataGrid1.SelectedItem.Cells(0).Controls(0),
    > >
    > > DataBoundLiteralControl).Text
    > >
    > > Returns Referenced object has a value of 'Nothing'.
    > >
    > > Mydatagrid.selecteditem returns Nothing and Mydatagrid.selectedindex

    = -1
    > >
    > > What does the reference item need to be changed to?
    > >
    > > "Ken Cox [Microsoft MVP]" <> wrote in message
    > > news:OL%...
    > >> Check this page and click the link for The DropDownList Scenario:
    > >>
    > >>

    > >

    http://msdn.microsoft.com/library/d...y/en-us/dnaspp/html/creatingcustomcolumns.asp
    > >>
    > >> Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
    > >> ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
    > >> Handles DataGrid1.ItemDataBound
    > >> If e.Item.ItemType = ListItemType.EditItem Then
    > >> Dim DRV As DataRowView = CType(e.Item.DataItem, DataRowView)
    > >> Dim CurrentShip As String = DRV("ShipVia")
    > >> Dim DDL As DropDownList = _
    > >> CType(e.Item.Cells(4).Controls(1), DropDownList)
    > >> Dim SQL As String = _
    > >> "SELECT ShipperID, CompanyName FROM Shippers ORDER BY ShipperID"
    > >> Dim DA As SqlDataAdapter = New SqlDataAdapter(SQL, ConnStr)
    > >> Dim DS As New DataSet
    > >> Dim item As ListItem
    > >> DA.Fill(DS, "Shippers")
    > >> DDL.DataSource = DS.Tables("Shippers").DefaultView
    > >> DDL.DataTextField = "CompanyName"
    > >> DDL.DataValueField = "ShipperID"
    > >> DDL.DataBind()
    > >> item = DDL.Items.FindByValue(CurrentShip)
    > >> If Not item Is Nothing Then item.Selected = True
    > >> End If
    > >> End Sub
    > >>
    > >>
    > >> "Robin" <> wrote in message
    > >> news:...
    > >> > In an asp.net page that has a datagrid, how do you change the data

    for
    > >> > that
    > >> > column to be listed in a listbox with the current value selected?
    > >> >
    > >> >
    > >>

    > >
    > >

    >
     
    Rick Spiewak, Aug 11, 2004
    #5
    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. SirPoonga
    Replies:
    2
    Views:
    820
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,075
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    550
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    318
  5. Replies:
    3
    Views:
    323
Loading...

Share This Page