Windows Form Combo Box display & values out of sync - c#

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

Related

How to go back to previously selected entry in a combobox?

In my application there is a requirement like this:
In a form there is a combo box and 4 textboxes. The combo box always contain range of values 1...10. Based on the value selected in the combo box, I need to read 4 data items corresponding to the selected value from a database and display these values in the 4 textboxes. Thus for each value in the combo box, there are 4 values in the database.
Also, user can change these values by editing the values in 4 text boxes.
As an example assume that currently selected value in the combo box is 1. So the data corresponding to record 1 is read from the database and shown in the 4 text boxes. If the user don't edit any value in the textbox and selects entry 2 (or any other value than 1) then the data corresponding to the newly selected value (i.e. 2) is fetched from the database and displayed in the 4 textboxes. But if the user edited any value by typing into any of the four available textboxes then those new values corresponding to record 1 should be saved to the database before switching to the newly selected entry, i.e. 2.
To do this I wrote some code in the "SelectedIndexChanged event handler" of the combo box. There I check whether the user has changed any value. If yes, I will prompt the user to save the data. After saving the data, the combo box will show the newly selected value by user i.e. 2 (and the 4 text boxes will show data corresponding to record 2).
All the above mentioned functionalities work fine. But there is an additional requirement "If the save operation to the database fails, then the initial value in the combo box should be shown along with the user edited data (in the textboxes) which means if the database write fails I have to show the currently selected value 1 without switching to the newly selected value 2".
In the SelectedIndexChanged handler I tried writing the below code.
If (DatabaseWriteFails)
{
ComboBox->SelectedIndex = previous_value; (previous_value is 1 in our example)
}
But the above code did not work (I think since we are trying to change the selected value from SelectedIndexChanged handler itself). When we enter the SelectedIndexChanged handler I can see that ComboBox->SelectedValue is already changed to the newly selected value i.e. 2.
The question is "how we can go back to the previous entry once we enter the SelectedIndexChanged handler"? Any help is appreciated. Thank you.
First of all, if you change the ComboBox value from the event, it will trigger that event again, so instead of using SelectedIndexChanged use SelectionChangeCommitted which occurs only when the user changes the selected item from UI, not programmatically.
Second, you should change the dropdown by using either SelectedItem or SelectedIndex, from my experience SelectedValue binding doesn't update the ComboBox.
My advice for you as mentioned in the comments is to use bindings or even ReactiveUI.

ListBox in devexpress gridview in c#

I have a gridview as you can see here:
As you can see i read the data in materialrequestcontractorId i have a listbox that this listbox reads its value from the database as you can see here:
List<MaterialRequestContractor> lstMRC = _materialRequestContractorRepository.Get().ToList();
foreach (MaterialRequestContractor VARIABLE in lstMRC)
{
LstMaterialRequestContractorId.Items.Add(VARIABLE.Id);
}
But the problem is i need to show my user the name of my MaterialRequestContractor not its id ,the id should be the value of my name and be saved in database .but in devexpress i can't assign this value and text to list box?
I have a record in my MaterialRequestContractor database with id=1
DevExpress provides several lookup editors for WinForms (LookUpEdit, GridLookUpEdit, SearchLookUpEdit) which support such scenario out-of-the-box. The idea is that LookUp editor has its own data source. When it is shown in the grid, it finds a value with from the grid cell in its data source (the key column determined by the LookUpEdit.Properties.ValueMember property) and shows the value from a field that selected as DisplayMember.
So if you put a table that has MaterialRequestContractor and MaterialRequestContractorId fields, and properly select DisplayMember and ValueMember, the DevExpress grid should work just like you described.

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

Getting changed values from combobox

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!

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