ComboBox returning System.Data.DataRow View For Every Value
I am building a point of sales software in C#. For adding a new product, I have created a combo box that would show all the categories from the mysql server. However, when I run the program only 3 values show up in my combo box and all of them have the same data: System.Data.DataRowView. Here’s my code. Any help would be appreciated.
private void LoadDataIntoComboBoxes() { CategoryComboBox.ValueMember = "Column_KeyValue"; CategoryComboBox.DisplayMember = "Column_DisplayValue"; CategoryComboBox.DataSource = GetComboBoxData(2).DefaultView; SupplierComboBox.DataSource = GetComboBoxData(3); } private DataTable GetComboBoxData(int listTypeID) { DataTable dtrecord = new DataTable(); using (SqlConnection con = new SqlConnection(ApplicationSetting.ConnectionString())) { using (SqlCommand cmd = new SqlCommand("usp_ListTypesData_LoadDataIntoComboBox", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@ListTypeID", listTypeID); con.Open(); SqlDataReader sdr = cmd.ExecuteReader(); dtrecord.Load(sdr); } } return dtrecord; }
you need to set ValueMember
and DisplayMember
from your databatable columns before fill the datasource with data :
CategoryComboBox.ValueMember = "Column_KeyValue"; CategoryComboBox.DisplayMember = "Column_DisplayValue"; CategoryComboBox.DataSource = GetComboBoxData(2);