Reputation: 1733
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
Upvotes: 0
Views: 428
Reputation: 23
I think this is being overthinked. AFTER setting your source, just set the .Text property of the combobox to "Select Items...". Sure the placeholder text is not actually added to the Items list but it achieves the desired results for the user right?
comboBox1.DataSource = ds.Tables["name"];
comboBox1.Text = "--Select Item--";
Upvotes: 0
Reputation: 2655
To solve database data binding issue -- you are not binding ComboBox data source correctly... re-order your lines like this..
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
Your first --select item--
problem.. To add a default item on top of your combobox, add following lines.. Make sure you add below line after binding datasource to your combobox.
comboBox1.Items.Insert(0, "-----Select item-----");
comboBox1.SelectedIndex = 0;
Edit: Solution that worked for me.
SqlConnection conn = new SqlConnection("Server = .\\SQLEXPRESS; Initial Catalog= Student; Trusted_Connection = True");
string query = "select Id, Name from abc1";
SqlDataAdapter da = new SqlDataAdapter();
conn.Open();
DataTable dt = new DataTable();
SqlCommand command = new SqlCommand(query, conn);
SqlDataReader reader = command.ExecuteReader();
dt.Load(reader);
DataRow Drw;
Drw = dt.NewRow();
Drw.ItemArray = new object[] { 0, "<----Select---->" };
dt.Rows.InsertAt(Drw, 0);
comboBox1.DataSource = dt;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
Upvotes: 1
Reputation: 3205
If I understood you correctly, you want to display some information in the ComboBox by default.
Try:
ComboBox1.Items.Add("Select Item");
It will add it as the first item in the list.
Upvotes: 1