View Full Version : default value for databound dropdownlist

Brett Bretterso
12-07-2005, 01:02 AM
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.

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";

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

Brett Bretterso
12-07-2005, 01:24 AM
I know you can do it like this:

ListItem newListItem=new ListItem();
newListItem.Text="Select a Person";
while (rdr.Read()){
newListItem = new ListItem();
newListItem.Text = rdr["fullName"].ToString();
newListItem.Value = rdr["id"].ToString();
, but I'm just wondering if an extra one can be added when you're using databind().

Brett Bretterson

12-07-2005, 03:02 AM
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;