Add blank item to dropdown list in datagrid

Discussion in 'ASP .Net' started by Big Dave, Jan 18, 2005.

  1. Big Dave

    Big Dave Guest

    I know it's been asked a million times before, but I still can't seem to
    find an answer that works.

    I've got a dropdown list in the footer template of a datagrid. The
    dropdown list databinds, but I can't seem to add a blank item to the top
    of the dropdown list.

    I've tried using the code to add this by placing it in both the
    page_load and the datagrid.itemDataBound event, but neither seems to
    work. I've also referenced the control by
    ctype(e.items.findControl("ddlName"), Dropdownlist) and by it's actual
    id. I've tried both the .add and .insert method's of the
    dropdownlist.items collection.

    I've been able to get this to work outside of the datagrid, but can't
    seem to get it to work inside the datagrid. Any help would be greatly
    appreciated!!!

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e
    As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    DataGrid1.ItemDataBound
    If e.Item.ItemType = ListItemType.Footer Then
    'option 1 - doesn't work
    CType(e.Item.FindControl("cmbAddFeatureType2"),
    DropDownList).Items.Insert(0, New ListItem("Select", ""))
    'option 2 - doesn't work
    Me.cmbAddFeatureType2.Items.Insert(0, New ListItem("Select",
    ""))
    End If
    End Sub

    Big Dave

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Big Dave, Jan 18, 2005
    #1
    1. Advertising

  2. Big Dave,

    If you want to add the item into the ddl inside the grid, you should use
    PreRender event. But I don't think it's the best idea. You can achieve the
    same effect by adding an empty row to the datatable the ddl is bound to.
    That's what I am doing in my projects.

    Eliyahu

    "Big Dave" <> wrote in message
    news:%23wNd1XW$...
    > I know it's been asked a million times before, but I still can't seem to
    > find an answer that works.
    >
    > I've got a dropdown list in the footer template of a datagrid. The
    > dropdown list databinds, but I can't seem to add a blank item to the top
    > of the dropdown list.
    >
    > I've tried using the code to add this by placing it in both the
    > page_load and the datagrid.itemDataBound event, but neither seems to
    > work. I've also referenced the control by
    > ctype(e.items.findControl("ddlName"), Dropdownlist) and by it's actual
    > id. I've tried both the .add and .insert method's of the
    > dropdownlist.items collection.
    >
    > I've been able to get this to work outside of the datagrid, but can't
    > seem to get it to work inside the datagrid. Any help would be greatly
    > appreciated!!!
    >
    > Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e
    > As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
    > DataGrid1.ItemDataBound
    > If e.Item.ItemType = ListItemType.Footer Then
    > 'option 1 - doesn't work
    > CType(e.Item.FindControl("cmbAddFeatureType2"),
    > DropDownList).Items.Insert(0, New ListItem("Select", ""))
    > 'option 2 - doesn't work
    > Me.cmbAddFeatureType2.Items.Insert(0, New ListItem("Select",
    > ""))
    > End If
    > End Sub
    >
    > Big Dave
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Eliyahu Goldin, Jan 18, 2005
    #2
    1. Advertising

  3. Big Dave

    Big Dave Guest

    Okay, I tried this, it still didn't work.


    Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles DataGrid1.PreRender
    Me.cmbAddFeatureType2.Items.Insert(0, New ListItem("Select",
    ""))
    End Sub

    I also tried it on the dropdownlist's prerender event, and it didn't
    work. It never even fired the event.

    I'm pretty hesitant about adding a blank record to my database lookup
    tables. I'm certainly no expert, but that just seems like a bad idea.
    Adding a blank item to the dropdown seems like it should be easy, but I
    just can't find a way to do it in the datagrid. So, I'm still looking
    for help, thanks!!!

    Big Dave

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Big Dave, Jan 18, 2005
    #3
  4. Big Dave

    vlo Guest

    vlo, Jan 22, 2006
    #4
  5. Big Dave

    Tomas Guest

    Tomas, Aug 2, 2007
    #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. Marina
    Replies:
    1
    Views:
    394
    Marty U.
    Jun 23, 2004
  2. Dica
    Replies:
    4
    Views:
    735
  3. QUASAR
    Replies:
    6
    Views:
    431
    QUASAR
    Jan 17, 2004
  4. M. Craig

    How to set background color of List item in dropdown list

    M. Craig, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    306
    M. Craig
    Nov 12, 2003
  5. M. Craig
    Replies:
    1
    Views:
    753
    Victor Garcia Aprea [MVP]
    Jan 9, 2004
Loading...

Share This Page