C# Combobox Displaying Blank Items - c#

I'm coding a combobox in C# and for some reason the items in the drop down don't have text. When I have selected an item, it is shown in the combo box text field (the drop down list is always blank whenever I click the drop down button). The datasource seems bound properly because the proper values are being returned when I select items, and the size of the drop down list will change depending on how many items the datasource has. Everything looks fine except for the fact that it seems like my drop down is populated with a bunch of empty strings, which it clearly isn't since as soon as an item is selected the proper text will display.
This is the relevant code:
if (list.Count > 0)
{
cboCustomers.DisplayMember = "Name";
cboCustomers.DataSource = list;
cboCustomers.ValueMember = "ID";
cboCustomers.SelectedIndex = 0;
}
I have looked for an answer to this but can't find it anywhere...I'm sure it's something really simple, but I can't figure it out. The closest problem I found had an answer suggested to set the display member before the data source, which clearly didn't work.
The list is populated from a database query. This will run on keyUp, the idea is that the list is populated as the person is typing based on the info given. So if I wrote 'S' I'd get a combobox with a dropdown that had all the clients starting with 'S'.

Given you don't have any anomalies in your binding, you are probably being affected by DrawMode property of your ComboBox, which may be set to OwnerDrawFixed or OwnerDrawVariable. Set it to Normal and things should get better.

as soon as an item is selected the proper text will display.
A foreground color the same as the background color will produce the same results you are seeing.

Related

keep combobox text synchronized with changed item text

I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?
I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.
Maybe I am missing something simple...
Edit
I tried binding, following the suggestion of SLak:
List<Label> labels;
MyComboBox.ItemsSource = labels;
The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:
labels[0].Contents = "new content";
then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.

Bound ComboBox not matching

I'm new to C# but not to programming. I've been toying around with VS, becoming more comfortable with the language and got stuck on a combobox bound to an int datatable column of an in-memory dataset.
I worked on a DataGridView and had no problems creating a DataGridViewComboBoxColumn and populating its dropdown list using a custom class comprised of Key:int and Desc:string. ValueMember = Key, DisplayMember = Desc. DataSource is left as null since the list is directly built into the ComboBox. Pick from the combobox and save changes works fine in the grid.
Then I moved to a ComboBox. Both the grid and the ComboBox have the exact same BindingSource. So yes they point to the same row at the same time.
I bind the ComboBox with:
cbCtrl.DataBindings.Add( "Text", myBindingSource, ColDBName );
Without populating the dropdown items of the ComboBox, I see the raw int value show up in the ComboBox. I change rows using the grid and the ComboBox value changes too. So it's good.
Then I populate the dropdown list of items in the ComboBox (the same as the grid's column tied to the same column) and it's trouble. The raw int value (eg 19) does not get matched to the proper "Key" item in the dropdown list of the ComboBox. I've tried DropDownStyle = DropDown & DropDownList but no luck with either. And I've looked for hours trying to find a fix. There are no events hooked up to the ComboBox. I expected automatic hookup of the raw value to the matching "Key" item in the ComboBox's dropdown list.
Is there something else to hook up? Frustrating since I expect something minor.
Any help would be Greatly appreciated. Thank you.
*Edit: The dropdown list actually comes from a populated BindingList<> and the BindingList<> is supplied as the ComboBox.DataSource.
I finally stumbled upon the answer:
cbCtrl.DataBindings.Add( "SelectedValue", myBindingSource, ColDBName );
That's all I needed. I got it from a great article
http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial?msg=5325959#xx5325959xx

How get the value of disabled ComboBox?

i have a lot of trouble with my CUIT. I want to test my window with data from database. The window is so constructed that some fields are deactivated so that the user can not change the values. The ComboBox gets a value, but in CUIT I can not read this value, because the control is not enabled. That's why I can not read properties of control right, SelectedIndex is f.e. always -1. Is there any way to find the text of ComboBox?
if you are working with WpfComboBox, you can obtain all items of the combobox by getting the property Items. It returns you basically UITestControlCollection and you can enumerate through its elements:
foreach (UITestControl uiTestControl in yourUiTestControlCollection)
{
string name = uiTestControl.FriendlyName;
}
or similarly by using lambda-expressions.
In this case it does not matter which item is selected and in which state (Enabled = true/false;) the combobox is.
However, it might not help if your combobox loads all the items somehow dynamically when changing its state to enabled. Then you will have to find another test scenario to make the combobox enabled before you analyze it with your test method.
Hope it will help.

ComboBox Items Empty but DataSource Full

After binding a list to combobox, its dataSource.Count is 5 but, combobox item count is 0.
how can it be?
I'm used to Web programming and this is in Windows Forms.
So no combo.DataBind(); method exists.
The problem here is, I'm trying to set the selected item programmatically. Since I don't see the combo.Items collection filled, I cannot set the desired item.
Update
A total update is needed I guess:
datasource contains 7 items
when bound to combobox, DisplayMember and ValueMember are appropriately implemented
after databound, through the gui, I can clearly see the 7 items in the combobox
combobox.DataSource.Count = 7 and combobox.Items.Count = 0
So the problem is here; since after databound no items are there in the ItemCollection of combobox; I cannot search for one to match and set the appropriate one.
Here is a image for better understanding (But I'm pretty sure I'm missing sth simple)
After adding ddl.BindingContext = new BindingContext(); before the BindingSource assignment, everything worked fine.
If you'd expand DataSource items in debuger, you'd probably notice that 1st element on list is null. That is why DataSource does not render ComboBox Items. Removing null items from the list should do all the work;
I had the same problem, but in my case it was caused by calling
combobox.Sorted = True
in InitializeComponent. I guess that call initializes Items, which then prevents the assignment to DataSource from updating it (Items).

DataGridViewComboBoxColumn - editing the Items

I am working with a DataGridView and have a column of type DataGridViewComboBox and I have stumbled across a problem. Basically, depending on the value of the cell, i would like to set the .Items to a certain set of strings, but when the value is changed, i would like to change the .Items list. But with this, I will occasionally remove a string from the list that is currently being occupied by another DataGridViewCell, this changes all the values that were equal to the removed item, until i re-add it back to the list (after i have finished editing).
So basically, I am wondering if there is a way of hiding some of the items from the combo box Drop Down list, so that when a certain cell is selected, they can't choose an item that isn't allowed.
Thanks,
Lloyd
Instead of databinding the entire column, databind each row's DataGridViewComboBoxCell individually. That way, you don't have .items from one row affecting .items from another row.

Categories