简体   繁体   中英

combobox value not showing in C#

I get problem while loading Combobox value from database. I am using SQLite Database. It only show a single value in Combobox but in my database there is multiple value. 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:-

 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:-

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. Please assign the DisplayValue along with 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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