I have a combobox that connect to my database in my window form application which work perfect. I am wondering if there any way to display any default text when displaying data from SQL Server?
For example: this is my comboBox while retrieving data from database
Name: item1
item2
item3
item4
item5
I want:
Name: -----Select item-----
item1
item2
item3
item4
item5
I have used this method
comboBox1.Select.Insert(0, "----Select Item");
comboBox.Select = 0;
For reference, here is my code to connect to mydb
// SQL Connection Configuration
try
{
// SQL Connection
myConn = new SqlConnection("Server = localhost; Initial Catalog= dbName; Trusted_Connection = True");
// Open Connection
myConn.Open();
myComboBoxCommand = new SqlCommand("select id, name from my_table", myConn);
myReader = myComboBoxCommand.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Load(myReader);
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
//Added This Here?
comboBox1.Items.Insert(0, "------Select Item-------");
comboBox1.SelectedIndex = 0;
comboBox1.DataSource = dt;
//Close Connection
myConn.Close();
}
This works, however, the combobox not able to display any data from the database. Any ideas? I have also looked at (this) form, which I don't think this is my case. Thanks