Bound ComboBox not matching - c#

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

Related

C# make combobox items remember datagridview.cell.value and datagridview.row of that cell

I have a Datagridview and 18 comboboxes.Each combobox represents a column of the Datagridview.
The point is: when I select a combobox item in the dropdownlist, I'd like the other comboboxes to select the corresponding item. I believe that to do that I have to make the comboboxes remember the row of the item.
At first I tried to make a specific class for it but I'm having some trouble.
Then thought that I could store the row information into the combobox by putting it into value property. But I'm still having troubles.
Keeping in mind that trouble won't ever completely leave me, someone has any tips?
Thank you all so much.
I would try this: use the SelectIndexChanged on the first combobox and get the line index, after that, change the others combobox index by setting them with the index you found. This is the dumbest approach, then you can improve it.

ComboBox in DataGridView WinForm not rendering selected value

I have a weird problem with ComboBox Column in DataGridView in WinForm.
When I select the item from ComboBox, the selected value would not be rendered by the ComboBox. I need to click the ComboBox so that the selected value of the ComboBox would be rendered or displayed. If not, the ComboBox would just display empty.
The weird thing is this only happen in my first ComboBox Column in DataGridView.
In other words, this issue is not happening with my Second, Third or etc ComboBox Column in DataGridView.
Any ideas? Could it be a DataGridView bugs?
Thanks for your attention.
Thanks for your attention.
I manage to find the solution for the question that I have asked.
Overview:
I populate rows in DataGridView manually by adding row into DataGridView.
For DataGridViewComboBoxCell object, I use DataSource to populate the Items instead of adding the Items manually to the
ComboBoxCell.
The Problem:
Whenever you select the item in ComboBox, it is unable to render the selected value in the ComboBox. It would only show the value if you click it.
Solution:
It turns out in the code somewhere after I set the DataSource of the DataGridViewComboBoxCell, I called its method, DataGridViewComboBoxCell.Items.Count. This is the source of the weird behaviour.
Somehow, if you have set the ComboBoxCell DataSource, and you call Items.Count method, it would show that weird behaviour.
I should not use Items.Count since I am using DataSource. The Items.Count would always be zero because the DataBinding it's not happened instantly the moment you set the DataSource.
Hopefully this post would help someone else in the future. Thanks.

C# Combobox Displaying Blank Items

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.

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).

Displaying a value in a populated combo box

Here is the situation:
I have populated a combo box with the names of divisions in my company and it is working fine. I go into edit mode and pull one record out of the data table so I bind all controls on the form with one record. I also populate this combo box with all divisions but want it to display the selected division. While I know how to display correct date in textbox controls, I do not know how to make combo box display only selected data. It displays the first record from the query which populates it. Any suggestions?
Thanks
Not clear if you are using ASP.NET or Windows Form. I am assuming Windows Form at the moment since it has an actual ComboBox control, while ASP.NET only has DropDownList (not counting the AJAX Control Toolkit).
ComboBox has a bunch of Selected... properties, i.e. SelectedIndex, SelectedItem, SelectedValue, SelectedText that you can manipulate (set) to show a certain item on the screen. So you can just do cbDivision.SelectedText = myRecord.Division (assuming Division in myRecord contains the same name as the one bound in the ComboBox.
for reference, see: this
I'm not sure I understood your question correctly. But I think you just want to display the selectedValue in the dropdown list populated from a list of values.
< asp:DropDownList DataTextField="SomeDecsription" DataValueField="SomeValue" ...... />
I apologise if this is not what you were asking for

Categories