Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框值未在C中显示#_C#_Sqlite_Datatable_Combobox - Fatal编程技术网

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
?否则,您只是将列表分配给组合框的数据源。