我正在Windows应用程序中使用Metro UI ComboBox。我只想在绑定数据库之前添加此“请选择名称”。我正在使用此代码。
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();
}
在显示绑定数据库数据之后,该组合框首先显示此“请选择名称”的方法。
答案 0 :(得分:1)
您可以执行以下操作:
private void fill()
{
comboBox3.SelectedIndex = -1;
List<string> temp = new List<string>();
temp.Add("Please Select Name");
temp.AddRange(comboclass.GetUserNames());
comboBox3.DataSource = temp;
}
尽管您需要记住,如果comboBox3.SelectedIndex == 0,则用户没有做出任何选择。