C # .NET ComboBox Control

I have an application running on a handheld device with a .NET compact framework using C # .NET. The problem I am facing is the ComboBox control behaving very awkwardly, sometimes it shows the content, but sometimes it just shows a string like System.DataRow in the ComboBox. Although it shows this System.DataRow line, it still has its values. I have a selectedIndexChanged event on the combobox and it returns the correct value, even the UI shows System.DataRow as display.

Thanks in advance. Greetings.

+2


source to share


2 answers


Thank you for your help. I figured out the problem. So the scenario was to populate a combo box with values ​​from the database. The database query was returning me three column ids, name, size. But I only needed 2 Name and ID columns for the combobox. I was getting the values ​​from the database and then adding a new client-side row to this datatype before filling in the combobox. In this client side, I only added 2 columns Name and ID, but not size. And this thing filled everything. Its weird but it works great. I think I'll do some more tests and see if this error occurs again.



Thanks for the help anyway.

+1


source


In the code that loads your ComboBox, you probably have something similar to this:

foreach (DataRow row in YourDataTable.Rows)
{
    YourComboBox.Items.Add(row);
}

      

Basically you load each DataRow into its ComboBox and the ComboBox uses the default DataRow ToString () value, which is "System.Data.DataRow". You need to load a ComboxBox with one of the DataRow fields instead, for example:

foreach (DataRow row in YourDataTable.Rows)
{
    YourComboBox.Items.Add(row["column1"].ToString());
}

      

Refresh . You may have a typo in the DisplayMember property. This code:



DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add(1, "Bob");
dt.Rows.Add(2, "Doug");
dt.Rows.Add(3, "Beth");
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = dt;

      

works correctly as expected, but DisplayMember is case sensitive, so if I change the second-last line to:

comboBox1.DisplayMember = "name";

      

all items in the ComboBox say "System.Data.DataRowView". I think you just need to check the column names.

+3


source







All Articles