How to go back to previously selected entry in a combobox? - c#

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.

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

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!

How to stop entering the text, when user going out of autoCompleteMode text c#

Iam using AutoCompleteMode to my textbox. It will append the Bank names to my textbox. So when I started typing with the first leter all the Bank names with first lettre will come to dropdownlist to my textbox. Now my question is
If user try to entre the data which is not im my dropdownlist, the user should not able to entre the text. So i want user to entr the existing bank names only.
Iam using AutoCompleteCustomSource to the textbox for dropdown.
try something like:
bool foundSome = false;
foreach (var bankName in col)
{
foundSome = bankName.StartsWith(textBox.text);
}
if (foundSome)
//Do some action
You can write this code in 'Validating' to preform for each char inserted in txtbox.
the best way to achieve your requierement is to use 1 textbox and 1 combobox. They both should point to the same collection.
Textbox will behave in autocomplete mode as you described. Once you type, combobox value will be set to first matching value from your collection. If no value matches - combobox value should be set to null or default data.
Combobox will store only corresponding data subset with no possibility to edit the chosen text.
Validation and data retrieval will be done from Combobox value.
Advantages of this approach:
- With large sets of data it will be easier for user to find what he/she needs.
- Lesser code to check if input value belongs to data set or to force belonging.
- No validation is needed.
Possible shortcomings:
- One more control at form.
- Logic to synchronize textbox's text and combobox collection should be implemented.

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

Stuck with datagridview and combo box column

I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

Categories