0

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

Community
  • 1
  • 1
RedRocket
  • 1,643
  • 7
  • 28
  • 51

3 Answers3

1

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.

Harry
  • 3,031
  • 7
  • 42
  • 67
  • Hi harry, thank you for your responds. This is what I meant to do, but when I data binded with my database, the combobox only show the data in the table without that `"Select Item"` – RedRocket Sep 14 '15 at 02:27
  • you need to add this before binding to the combobox. – Harry Sep 14 '15 at 02:52
  • You mean to add this before the `DataTable dt = neew DataTable();` right? I have tried this, this only give me the data that I want to bind :( – RedRocket Sep 14 '15 at 03:07
  • Instead of binding, why don't you loop through the reader and add each item one by one. You can something like while(reader.Read){//add each item to combo box} – Harry Sep 14 '15 at 03:19
1

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";
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • Hi Amnesh, Thanks for reply, I have edited my answer, but the combobox only showed `----Select item---- `now without any data – RedRocket Sep 14 '15 at 02:31
  • @DragonBorn Are you getting values in your data table? – Amnesh Goel Sep 14 '15 at 04:37
  • This line of code allow me do show my header while binding data it at the same time – RedRocket Sep 14 '15 at 04:39
  • @DragonBorn why did you try that? Are you getting values in SQLDataReader? – Amnesh Goel Sep 14 '15 at 04:40
  • I am not sure, I am just playing around with it and it work :/. I've tried your answer but unfortunately it doesn't work. – RedRocket Sep 14 '15 at 04:41
  • when I am trying to hover my mouse to SelectedIndex, the value said to be "-1". I am not sure why – RedRocket Sep 14 '15 at 04:44
  • Can you try once following .. `ComboBox.Items.Add("--Select Item--") ComboBox.DataSource = Dt ComboBox.DisplayMember = "Name" ComboBox.ValueMember = "Id" ComboBox.DataBind() ` ComboBox.SelectedValue = "--Select Item--" – Amnesh Goel Sep 14 '15 at 04:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89539/discussion-between-amnesh-goel-and-dragonborn). – Amnesh Goel Sep 14 '15 at 04:48
0

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--";
NadJ
  • 13
  • 1
  • 5