Databound Combobox not unfocusing - c#

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

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!

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.

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.

How to persist/store values at runtime

I have a scenario which is as follows:-
A form containing 2 grids. The grid on the left contains a list of groups. When a group is selected the grid on the right populates with another list with check boxes.
I would like to be able to select group A and select some random check boxes and then switch to group B and select some other check boxes. However when I select group A again I would like to be able to restore the previously selected check boxes.
This would allow me to preload the settings from the database and also update the changes in one go rather than expecting the user to select apply after the changes for each group.
I'm unsure of the best way to approach this problem. Any feedback is appreciated.
Thanks
Sean.
I've done something like this using DataTables and DataViews to implement client-side filtering.
You have a dataTable with the tickboxes that contains all groups of data.
Then when you click grid 1, you update the DataSOurce of the grid to a new DataView(DataTable,"GroupID=1")
,"",CurrentRows) (i.e. Sets the filter proprety of the DataView to filter on the selected group
THen when you're done, the DataTable has all the tick boxes you just saev the DataTable to the database.
Hope that makes sense.
I'd start with coming up with a database table to store your data - let's say MyTable(UserId, GroupId, ItemId, Selected). When the app starts I'd read the data from the table based on the UserId. I'd process this data into a couple of collections that I could associated with the grids - one having unique groups, the other items associated with a group and the selection status. Set up some event handlers to keep the collections in sync with the user input. Implement some save routine upon hitting a button or form closing and you should be good !

Categories