组合框值未在C#中显示

时间:2018-05-10 03:11:17

标签: c# sqlite datatable combobox

从数据库加载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);
            }
        }

1 个答案:

答案 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";