Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    default value for databound dropdownlist

    I'm trying to have a dropdownlist that's populated from a database using databind that has a default value that doesn't come from the database.

    Code:
    qryStr="select id, lName+', '+fName as fullName from people";
    			OleDbCommand cmd = new OleDbCommand(qryStr, cn);
    			OleDbDataReader rdr = cmd.ExecuteReader();
    			
    			selectPerson.DataSource = rdr;
    			selectPerson.DataTextField = "fullName";
    			selectPerson.DataValueField = "id";
    			selectPerson.DataBind();
    let's say this makes it so the dropdownlist has Pizza Head and Lasagna Face, but I want to make it so at the top of the list it says something like "Select a Person". How can I do that?

    Brett Bretterson

  • #2
    New Coder
    Join Date
    Aug 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know you can do it like this:
    Code:
    ListItem newListItem=new ListItem();
    newListItem.Text="Select a Person";
    newListItem.Value="0";
    selectPerson.Items.Add(newListItem);
    while (rdr.Read()){
        newListItem = new ListItem();
        newListItem.Text = rdr["fullName"].ToString();
        newListItem.Value = rdr["id"].ToString();
        selectPerson.Items.Add(newListItem);
    }
    , but I'm just wondering if an extra one can be added when you're using databind().

    Brett Bretterson

  • #3
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    After you've bound your data, you can use the DropDownList.Items.Insert(index, item)

    Therefore, you can just specify 0 as the index and it should put it at the begining.

    Good luck;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •