0

i'm using Metro UI ComboBox in windows application. i just want to add this "Please Select Name" before bind the database. i'm using this code.

 public static List<string> GetUserNames()
    {
        using (SQLiteConnection conn = new SQLiteConnection("Data Source=combolist.db;Version=3;"))
        {
            string CommandText = "SELECT Id FROM combo ORDER BY Id";
            using (SQLiteCommand cmd = new SQLiteCommand(CommandText, conn))
            {
                conn.Open();
                DataTable dt = new DataTable();
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                da.Fill(dt);
                return dt.Rows.Cast<DataRow>().Select(dr => dr["Id"].ToString()).ToList();
            }
        }
    }



private void fill()
    {
        comboBox3.SelectedIndex = -1;
        comboBox3.DataSource = comboclass.GetUserNames();
    }

how to do that combobox show this "Please Select Name" first after that show bind database data.

john
  • 11
  • 1
  • [Set hint or watermark or default text for ComboBox without adding it as item](https://stackoverflow.com/a/50972125/3110834) – Reza Aghaei Jan 15 '19 at 03:19

1 Answers1

1

You could do the following:

private void fill()
    {
        comboBox3.SelectedIndex = -1;
        List<string> temp = new List<string>();
        temp.Add("Please Select Name");
        temp.AddRange(comboclass.GetUserNames());
        comboBox3.DataSource = temp;
    }

Although you will need to keep in mind that if comboBox3.SelectedIndex == 0 then the user has not made any choice.

Alex Leo
  • 2,781
  • 2
  • 13
  • 29
  • [Set hint or watermark or default text for ComboBox without adding it as item](https://stackoverflow.com/a/50972125/3110834) – Reza Aghaei Jan 15 '19 at 03:19