从数据库加载Combobox
值时遇到问题。我正在使用SQLite
数据库。它只显示Combobox
中的单个值,但在我的数据库中有多个值。那我怎么能这样做呢。
这是我的代码:
类DatabaseHandler
它有两个方法Execute Query
和其他方法返回DataTable
个对象: -
public int ExecuteSql(String Query)
{
SQLiteCommand command = new SQLiteCommand(Query, Connection);
return command.ExecuteNonQuery();
}
public DataTable GetDataTable(String Query)
{
SQLiteCommand command = new SQLiteCommand(Query, Connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
return dataTable;
}
这是Category
类,这是我遇到问题的一段代码: -
private void CategoryName()
{
try
{
String Query = "Select CategoryName from CategoryTable;";
databaseHandler.ExecuteSql(Query);
DataTable dataTable = databaseHandler.GetDataTable(Query);
ProductType.DataSource = dataTable;
ProductType.DisplayMember = "CategoryName";
}
catch (Exception CatID)
{
Console.WriteLine(CatID.StackTrace);
}
}
答案 0 :(得分:0)
我认为您分配的是DisplayMember而不是ValueMember。请指定DisplayValue和ValueMember。
List<Country> countries = new List<Country> { new Country("UK"),
new Country("Australia"),
new Country("France") };
bindingSource1.DataSource = countries;
comboBox1.DataSource = bindingSource1.DataSource;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";