Getting changed values from combobox - c#

quick question, let's say I have a combobox in a windows form, that get's filled with data from a sql table, and when a value is selected, it fills, let's say a textbox or a label with data from the same row. What I need (and can't get to make it work properly) is that each time I select a different value on the combobox, it will reflect those changes on the hypothetical textbox or label, without pressing any extra control, just by changing values on the combobox. For example:
Let's say I have this data on an SQL column and it's showing on a combobox on a windows form:
London
New York
Paris
etc.
and each time I select one city from the combobox, I want get that data from sql and show those changes on a textbox that has the data of the zip code, country, etc of each city.
What I'm currently using is the SelectedValue property that works fine th first time you select an item but doesn't change the data on the other controls when you select another value the second time.
Do you have any idea on how to implement this or a better way to do it?
Thanks!

Related

Windows Form Combo Box display & values out of sync

I have developed a small application but ran into this problem, so I'm starting with a clean slate to try and isolate it. So far, I made a very simple Windows form (VS 2017) application connecting to an SQL data table. On the form, I have a combo box for selecting a table row that is bound to the data set. I have it displaying an order number, and have its value member as an EntryID number as shown, which is also the "selected value".
I also have a simple text box on the form displaying the EntryID.
When I run the app, the combo box initially displays an ordered list of order numbers like this:
Before selecting an item, if I scroll through the list using the form's tool bar selector, the EntryID text box value corresponds to the combo box value, so Ord40 selects the last data set row (where EntryID = 1003).
When an item is selected, however, the combo box list order changes. For example, after a few selections, I get:
But if I select the last display item, "Ord 20", I still get the data row EntryID = 1003. In other words, although the bound data set does not change, the combobox scrambles the displayed item. Put yet another way, if the combobox has a set of display fields and a corresponding set of value fields, the display field text get out of sync with the underlying value.
I hope that makes sense. This is straight, out of the box, no altered code on a fresh project, and I have tried different settings on the "selected value" property.
Any help would be much appreciated.
James
You are binding SelectedValue of the ComboBox to the same data source which you use as data source of the control. Setting DataSource is just for showing items in dropdown. Setting SelecetdValue if for changing the value of bound property/column.
You never want to bind SelectedValue to the same data source which you use for DataSource.
Just as a an example, assuming you have the following tables:
Product (Id, Name, CategoryId, Price)
Category (Id, Name)
Then if you want to show a ComboBox for CategoryId in the edit form of Product, the setup of the categoryIdComboBox should be:
DataSource: categoriesDataSource
DisplayMember: Name
ValueMember: Id
SelectedValue: productsBindigSource - CategoryId

Databound Combobox not unfocusing

I'm trying to make a databound combobox in a bindingnavigator. I want the options for the combobox to come from a different table and have a different display and value member.
So I have a member table and a group table and each member has a group determined by a fk_Group column. I want the combobox to be able to select from the currently existing options in the group table to set the member fk_Group to one of the pk_Groups that already exists.
So far I have the combobox properties set to
datasource: groupbindingsource
Display Member: GroupName
ValueMember: pk_Group
CausesValidation: False
Databinding SelectedValue: memberBindingSource - fk_Group
This makes sense to me and the combobox populates correctly but when I click an option I can't remove focus from it and the drop down starts behaving oddly, immediately snapping back up when it comes down.
I can get out of it by using the bindingnavigators arrows to go to a different record but then it clears the combobox.
Ideally I would like the combobox to populate with the current group name for that member as well as be able to select from a list of the other groups.
Thanks

Is two data sources for a DataGridView.ComboBoxColumn possible?

I have a DataGridView with several TextBoxColumns and one ComboBox column called 'combo' that holds the client type. The problem is that I'd like to show both the currently selected client-type value along with the dropdown client-type list to validate future changes by the user. In SQL Server, I have a DB with two table columns, 'client_type_dropdown.name' and 'clients.client_type'. The 'client_type_dropdown.name' column is a validation list. The 'clients.client_type' column contains the current client type for clients in the database. Is there a way to show in 'combo' both 'client_type_dropdown.name' and 'clients.client_type' , i.e., one source for the ComboBoxColumn dropdown and a different source for the textbox part of 'combo'? Or do I need to have two columns in my grid?
I appreciate your help.
I'm using a third party grid, but I usually handle this by setting the combo drop down style to DropDown instead of DropDownList. This will allow your original database value to display, even if it isn't in the list.
This also allows free typing of values into the combo field, so the trick after that is to validate the user input to make sure it matches a value in the list before you allow them to save updated values. You could play around with the LimitToList property of the combo to possibly save you doing the validation manually, but with most controls I have worked with it will give you more grief than help.

How to make a combobox column show only unselected items?

It's supposed to be a very common workflow, so I am surprised that neither C# gridview or DevExpress provides an easy way to do it.
Example:
MyColumn is a field of combobox type column. When a user tries to edit value in this field, he will have to choose a value in the dropdown list. When the grid has 1 row, he can choose from "value A", "value B" or "value C". If the user has chosen "value A" in the 1st row, then in the 2nd row the available values for MyColumn become "value B" and "value C".
Currently I am doing it in a dirty way with DevExpress , where I calculate the combobox repository (modify RepositoryItemComboBox.Items property) everytime after a delete, insert or modify operation, and in the CustomRowCellEdit event handler I assign the updated repository to the column. The code is very convoluted and brittle.
------update------
I just came up with a better example of a use case:
Say that the grid asks the user to select his 5 most favorite US states. So the column State is a combobox type column that initially has a list of 50 states. If the user selects CA for the 1st row, then in the 2nd row he will have only 49 states available to select. In this case, you always want values to be unique in each row or it would not make sense.
You might have thought of it; but just in case if you haven't:
I believe, rather than trying to change the repository every time, better to actually validate the selected value in the combo (in the onchange event of the combobox) and override the user selection for this item in case it is a duplicate.
Note: Also, changing the repository, I think, will have it's own share of issues; as changing the DataSource after every delete, insert or modify will alter the data in other combo boxes (Obviously, here I am assuming that all combos uses the same Datasource).
I've got a solution.
Handle the CustomRowCellEditForEditing event. In the handler, traverse all rows and get all selected items, and remove the selected items from the repositoryItemComboBox1.Items (suppose that's your column editor).
Of course, the logic is a little more complicated because you will also have to change the Items collection when user edits or deletes a row.
Caution:
CustomRowCellEdit is the wrong way to go. Because everytime RepositoryItemComboBox.Items is changed the event will fire.

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