I have a data-bound ComboBox and I populate a DataSet with the values returned from a SQL query.
Expand|Select|Wrap|Line Numbers
- public DataSet CompanyArray(DataSet dataSet)
- {
- SqlConnection connection = InstancesTest.IDataInterface.DefineConnection();
- SqlCommand command = new SqlCommand();
- command.CommandText = "usp_CompanyArray";
- command.CommandTimeout = 20000;
- command.CommandType = CommandType.StoredProcedure;
- command.Connection = connection;
- try
- {
- if (connection.State != ConnectionState.Open)
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- DataTable dt = new DataTable();
- dt.Load(reader);
- DataRow row = dt.NewRow();
- row["COMPANY_NAME"] = "Please Select";
- dt.Rows.InsertAt(row, 0);
- dataSet.Tables.Add(dt);
- reader.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- if (connection.State != ConnectionState.Closed)
- connection.Close();
- }
- return dataSet;
- }
Expand|Select|Wrap|Line Numbers
- void CompanyArray()
- {
- DataSet ds = new DataSet();
- ds = logic.CompanyArray(ds);
- string[] array = new string[ds.Tables[0].Rows.Count];
- int counter = 0;
- foreach (DataRow dr in ds.Tables[0].Rows)
- {
- array[counter] = dr["COMPANY_NAME"].ToString().Trim();
- counter++;
- }
- Array.Sort(array);
- foreach (string companyName in array)
- {
- cmbCompanySelect.Items.Add(companyName.ToString().Trim());
- }
- //cmbCompanySelect.SelectedItem = "Please Select";
- int i = cmbCompanySelect.Items.IndexOf("Please Select");
- }
What I'm after is putting a "Please Select" at index 0 in the ComboBox. How can I move this value to the top of the list, without reordering the sorted list?
And I don't really want to insert a "Please Select" as a first row in a database table. Any ideas?
Thanks