English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a ComboBox which I fill with values from a database table.

When the form containing the control loads, I want it to have an initial selection like "-- Please select --". To accomplish this, it seems like I need to first add this initial item manually to the control, and then set the SelectedIndex property to 0. After that, I would fill the control with the rest of the data.

Will this work, or is there a better way? Examples would be helpful.

2007-03-08 17:01:21 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

Rex M, thanks; one question before I award the points, will doing the insert as you describe overwrite the existing data element, or will it be inserted before the existing data set?

2007-03-09 01:21:03 · update #1

Rex M nevermind, I found a way to do it based on your info. Here's the code:

DataSet dsCodes = new DataSet();

SqlConnection cnCodes = new SqlConnection(connectionString);

SqlDataAdapter daCodes = new SqlDataAdapter(commandString, cnCodes);

daCodes.Fill(dsCodes, "Codes");

cboCodes.DataSource = dsCodes.Tables["Codes"];

DataRow dr = dsCodes.Tables[0].NewRow(); // create a new row into the dataset
dr[0] = 1102; // supply values
dr[1] = "-- Please select --"; // supply values
dsCodes.Tables[0].Rows.InsertAt(dr, 0); // Insert the row at index 0

cboCodes.SelectedIndex = 0;



cboCodes.DisplayMember = "CodeDescription";

2007-03-09 02:09:51 · update #2

2 answers

No, that's pretty much the way to do it - except that you should fill the control with the data first, then insert your custom item manually. This way you can use the built-in databinding functionality without worrying about clearing the existing data (including any items you've added).

ctrl.DataSource = sqlComm;
ctrl.DataBind();
ctrl.Items.Insert(0, new ListItem("-- Please select --",""));
ctrl.Items[0].Selected = true;

2007-03-08 17:09:03 · answer #1 · answered by Rex M 6 · 0 0

there's a mixture field journey called "chosen index replace". whilst that journey happens you pick get the chosen index(which could be unique) and open or set to lively the corresponding type..

2016-11-23 16:45:38 · answer #2 · answered by ? 4 · 0 0

fedest.com, questions and answers