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