Displaying a value in a populated combo box - c#

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

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!

Editable Combo Box in Datagrid of WPF (C#)

I am developing sale invoice in WPF. Previously, I am using pop up to manage add, edit and delete operation in datagrid but now I want to do all of these operation in DataGrid. Everything is possible except an editable Combo Box. There are following fields in Grid:-
Description
Qty
Rate
Unit
Total
There should be a editable Combo Box in description filed where from I select an item and rate and unit should be accessed from product table and automatically add in specific cell of grid.
Combobox (as type any character in combo box, item should be short list or cursor move to specific record). I am able to add such functionality outside the grid but unable to add in grid.
I think you just need to add this code to your ComboBoxItem
IsEditable="True"
This would make it editable, and GridView has nothing to do with that.

Adding checkbox controls to list box

I'm new to .NET environment and just a student.
I'm working on User Management. I want to assign multiple roles to one user. For this purpose I've created list box which contains a list of roles from database.
lbRoles.Items.Add(readerRole["RoleName"].ToString());
I just need a check box with each item. Please suggest how to add a checkbox with each item.
I did tried
lbRoles.Controls.Add(checkBox);
lbRoles.Items.Add(readerRole["RoleName"].ToString());
But it was not helpful. I did google but no result :(
There is the CheckedListBox class, its very simple and does exactly what you want. :)
Displays a ListBox in which a check box is displayed to the left of each item.
Instead of using a ListBox, use a ListView instead and set ListView.Checkboxes to true.
This will place a CheckBox next to each item in the ListView, and your users can select specific items in the ListView by clicking the checkboxes, then get the selected items using ListView.SelectedItems.

How do I create an editable databound combo box in a DataGrid in WinForm?

I have a noob question.
I have a WinForm (.net 2.0 in VS 2008) on my form I have a DataGrid. One of the fields in that DataGrid is a combo box that is bound to a separate table. This works, but I am unable to edit or add. I cannot add a value that is not on the list.
I want to create a lookup box (for lack of a better term).
The form is for Parts Order Entry. In the DataGrid Data Source is PartsOrder_table, the Work order Field is an int in the PartsOrder_table, and the combo box is bound to the WorkOrder_table. The WorkOPRder_Table has WorkOrd_ID int and WrkOrd nvarchar(10)
The Combo Box Data Source is WorkOrd_Table Display Member is WorkOrd Value Member is WorkOrder_ID.
This works great with the problem that I cannot add or select anything not in WorkOrder_table.
Any help would be greatly appreciated.
Seems you cannot in a straight forward manner. This thread shows a way of doing this, by adding the needed values during the Validation of the ComboBox control. See the URL for more details.
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/31a28e99-6f9f-4cce-b256-9b7cf1ddf69a/

Categories