简体   繁体   English

组合框值未显示在C#中

[英]combobox value not showing in C#

I get problem while loading Combobox value from database. 从数据库加载组合Combobox值时遇到问题。 I am using SQLite Database. 我正在使用SQLite数据库。 It only show a single value in Combobox but in my database there is multiple value. 它仅在Combobox显示一个值,但在我的数据库中有多个值。 So how can i do this. 那么我该怎么做。

This is my Code: 这是我的代码:

Class DatabaseHandler it has two methods which Execute Query and other methods return DataTable object:- 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;
          }

This is Category Class and this is piece of code where i got a problem:- 这是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);
            }
        }

I think you are assigning a DisplayMember but not ValueMember. 我认为您分配的是DisplayMember,而不是ValueMember。 Please assign the DisplayValue along with 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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM