Combobox saves text to database instead of value - c#

I'm using Visual Studio, so at this point I have not written any code, it's all just property settings and drag-n-drop in the GUI. I have a form with several controls bound to a table. To populate a ComboBox on the form , I used the ComboBox task panel to bind the options to a view that has two fields (Type_Num and Type_Desc).
In the properties box for the ComboBox, I have my DisplayMember set to Type_Desc and my ValueMember set to Type_Num. When I run the form, the correct values are there in the dropdown, but when I select one and save, it tries to save the Type_Desc in the control instead of the Type_Num. What am I missing?

This happens when you have setup data-binding to Text property, while you need to save selected value. You should bind SelectedValue.
To change it at designer, expand the (+DataBindings) property group and remove data binding from text property and setup data binding to SelectedValue:

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

Devexpress MVC GridView ComboBox Column Type DataSource

How to alternate DataSource in ComboBox Column inside Devexpress GridView by selecting value in other ComboBox column in same row.
It is something like cascading comboboxes but in Grid view (but in grid comboboxses don't have CallbackRouteValues property)
Grid has two comboboxes and if user selects one value in frst the second combobox must change datasource by function that takes id as parameter from first selected combobox
You want to use a LookUpEdit instead of ComboBox when you are using a datasource, then you can use the QueryPopUp event to set a filter to, or change your datasource.
Note that when you change the datasource for the LookUpEdit, the result in the grid might not look what you expected. To remedy that, you want to clear your filter in the QueryCloseUp event.

How to get data from database to CheckboxList control inside a form view control?

I am working on asp.net user control. I have used a gridview and a formview control.
on selecting a row in gridview it will hide the panel containing the grid and will display the panel containing the form view which is using the Grid's selected value as its key value and form loads in edit mode. for some extra use i had to place a checkbox list control in my form view control. and used SQL datasource to fetch data from databese to chkbox list. and used the same data key as formview control. Now my form view control works properly but my Checkbox list is not working properly as it cant get the selected value from grid view.
Thanks in advance for help.
You should bind your CheckBoxList on the DataBound event of your FormView.
Since you would need the same data key, you could use the DataKey property of the FormView.
If any additional data fields are required, you always have the DataItem property.
Done in this order, your CheckBoxList should work as expected.
I used a session variable and it worked for me.
i used a session variable to store the selected value of grid in its selected index changed event and then used it as key value in checkbox list.

Typing text in Combobox placed inside Datagrid

How to allow Combobox placed inside Datagrid to type text.
It just binding the values already stored in the database. but i need to type the value in the Combobox which is not shown in the bound values.
How to make the Combobox as editable column?
You can set ComboBoxStyle to DropDown
Specifies that the list is displayed by clicking the down arrow and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.

Problematic comboBox Windows Forms

I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should.
In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.
private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBoxImageSet.SelectedText);
}
When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written.
Why?
Thanks.
Here the differences between the selection properties of a ComboBox control.
SelectedIndex;
SelectedItem;
SelectedText;
SelectedValue.
The SelectedIndex property :
Gets or sets the index specifying the currently selected item.
Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))
The SelectedItem property :
Gets or sets currently selected item in the ComboBox.
The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.
The SelectedText property :
Gets or sets the text that is selected in the editable portion of a ComboBox.
That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.
The SelectedValue property :
Gets or sets the value of the member property specified by the ValueMember property.
Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))
The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.
What you are interested in is the SelectedItem property or the SelectedValue property.
ComboBox.SelectedText
A string that represents the currently
selected text in the combo box. If
DropDownStyle is set to DropDownList,
the return value is an empty string
("").
Use SelectedItem instead of SelectedText
SelectedText:
Gets or sets the text that is selected
in the editable portion of a ComboBox.
That is, it gets the text that is currently marked.
You want to use SelectedItem.ToString().

Categories