C# 组合框值未在C中显示#
我在从数据库加载C# 组合框值未在C中显示#,c#,sqlite,datatable,combobox,C#,Sqlite,Datatable,Combobox,我在从数据库加载组合框值时遇到问题。我正在使用SQLite数据库。它在组合框中只显示一个值,但在我的数据库中有多个值。那么我该怎么做呢 这是我的代码: 类DatabaseHandler它有两个方法executequery和其他方法返回DataTable对象:- public int ExecuteSql(String Query) { SQLiteCommand command = new SQLiteCommand(Query, Connection)
组合框
值时遇到问题。我正在使用SQLite
数据库。它在组合框中只显示一个值,但在我的数据库中有多个值。那么我该怎么做呢
这是我的代码:
类DatabaseHandler
它有两个方法executequery
和其他方法返回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;
}
这是类别
类,这是我遇到问题的一段代码:-
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);
}
}
我认为您分配的是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";
List countries=新列表{新国家(“英国”),
新国家(“澳大利亚”),
新国家(“法国”);
bindingSource1.DataSource=国家/地区;
comboBox1.DataSource=bindingSource1.DataSource;
comboBox1.DisplayMember=“Name”;
comboBox1.ValueMember=“Name”;
试试这个。ProductType.DisplayMember=“值”;ProductType.ValueMember=“Key”;还要检查数据表是否有行返回,它可能有空返回。请编写代码我已尝试使用ProductType.DisplayMember=“CategoryName”
<代码>ProductType.ValueMember=“CategoryID”;但它只显示单个值,而不是所有值。它不应该是comboBox1.DataSource=bindingSource1
?否则,您只是将列表分配给组合框的数据源。