I encountered a very specific behave on the Comboboxes in Winform. I found a lot of quite similar questions, but no useful anwser to my problem:
I have a form where controls are bound to a SQL datasource via TableAdapter/manager. Some of the values are linked to Lookuptables which are of type Combobox. When I first fill the base dataset, all is shown correctly. I can choose items from Combobox (fields id and NamedItem) where "id" is bound as datamember to the base datset table and "NamedItem" is the DisplayMember of the Combobox.
No problems when changing values or updating but ...
once I have to refill the Combobox underlaying datasource (eg. in case, an entry was added), the selected Index of the Combobox is set to 0 - means, the first item in the list.
Sure, I could remember the selectedindex and restore it after. For one Combobox, no problem, but for dozens ...
Is there any other way to keep the last selecteditem after refilling the combobox?
If items would not be deleted from your combo list , turning the Clearbeforfill property of your Table Adapter to False can prevent that.
Related
I have plumbed the deeps of my google-fu to find an answer.
First, I am not simply asking how to bind a combobox to a datasource. It's a bit more than that unless I'm having a serious understanding gap.
On my Winform, I have a DataGridview on the left and on the right I have a panel with values from the selected row on the left. One of those controls on the right is the ComboBox I'm having trouble with. I have my bindingsource and dataview set up and the other controls on the right are working splendidly, except the ComboBox control.
The user, interacting with this ComboBox, should see values such as "Item ABC" and "Item EFG" and the value related to them might be 1234 and 5678. If this was a fully unbound control I'd put an object array of items in. Once I get it working, I'd load that from a different source.
But when I try to DataBindings.Add("??", dataview, "dataviewfield", ...) I can't get the proper value for "??". Runtime debug shows that "SelectedItem.Value" would be the right option, but I get "not found" type exception when I use that. I've tried "SelectedValue" as well, but that didn't work (debug show's it's null & throws no nulls allowed exception).
How can I get that value placed directly into the DataView via the Binding?
Setting the .DataSource simply loses the items and doesn't help at all.
How does one do this? Short of making the ComboBox unbound totally, setting the selectedindex directly and capturing the value when the selected index changes changes - Just seems so clunky to have to do that.
-old programmer
Further Notes: I edited to clarify the placement of the ComboBox.
I have made progress (naturally, only after asking a question does a new avenue pop into my head). I got to thinking I might need a custom binding adapter so started googling that. I found some samples doing what I want.
The foremost problem was I was not using assigning a datasource on the ComboBox, I was simply adding items. When I created a two column dataset and a few rows (could have been anything I suppose) and set that and the two field names as the ComboBox's displaymember and valuemember did the SelectedValue start showing a value (instead of null all the time).
I think that was the problem. The remaining issue is getting the left hand side to re-display/refresh after the change.
When loading the datasource for the DataGridView and the bindingsource, try making them share the same list:
BindingSource1.Datasource = dataset
DataGridView.Datasource = BindingSource1
This should mean that any changes to the data in the bindingsource will also update the DataGridView.
To edit the selected object then just handle the SelectionChanged event and set the BindingSource1.Position to the index of the selected object in the Datasource.
I would like my comboboxes to have the first value be empty so that the user can clear their previous selection. The comboboxes are bound to entities in the ViewModel. So how do I add this first value. I could used combobox.inert(0, new Entity), but is that the correct way?
This is for searching purposes: By default the combobox has no selection and the search will find everything. If they select an item the search is filtered.
As your ViewModel is responsible for preparing the data to be displayed by the View, the ViewModel should add the empty element at the beginning of the collection that is then bound to the combobox.
I have a Combobox, in which I would like its items to be the column data that is located on a DataGrid. Is there anyway to set the Combobox itemsource to be a specific column of a DataGrid?
Right now I'm iterating each row of the DataGrid, getting the field's data and adding them to the Combobox, but that means that I would have to clear all the items and reiterate everytime the DataGrid is modified.
You can set ItemsSource and DisplayMemberPath properties:
comboBox1.ItemsSource = dataGrid1.ItemsSource;
comboBox1.DisplayMemberPath = "ColumnName";
I Think you'are taking the wrong approach. Your data grid must be bound to a collection of object. I guess you could just build another collection by extracting the desired fields (for example with linQ) and expose this new collection to your view such that you can bind your combobox.
I you want to keep this second collection updated, make your first main collection an ObservableCollection such taht your can subscribe to CollectionChanged Event. In the event handler, just manage the add and remove in your combobox source collection.
I have a comboBox that displays different Municipalities (these Municipalities belongs to a particular Province) in our country. Since there are Municipalities having the same name, I binded the "MunicipalityName" (a table column from 'MUNICIPALITY' table in my database) to DisplayMember property of the comboBox and "Municipality_ID" to ValueMember property of the comboBox.
When the user saves his details, I supply the SelectedValue from ValueMember of the MUNICIPALITY and insert it to Employee table.
cmd.Parameters.Add(new SqlParameter("#Municipality_ID", (object)comboBoxMunicipality.SelectedValue.ToString()));
I find it hard when it comes to retrieval of data when an Employee needs to update his information. I have to manually check the Municipality_ID of that employee and compare it to the binded data in the comboBox, then loop through it, determine what index that Municipality_ID located, and set the SelectedIndex property of the comboBox. (Quiet lengthy compared to code snippet below)
I have this code, but I find conflicts since Municipality_Name is not unique.
//set SelectedIndex based from DisplayMember of the comboBox
comboBoxMunicipality.SelectedIndex = comboBoxMunicipality.FindStringExact(dataTable.Rows[0]["MunicipalityName"].ToString());
Is there a way to set the SelectedIndex of the comboBox like the code above, but this time, comparing it to the ValueMember?
Is there a shortcut?
//something like this?
comboBoxMunicipality.SelectedIndex =
comboBoxMunicipality.FindByValue(dataTable.Rows[0]["Municipality_ID"].ToString());
I hope you get my point guys... Please help. Thanks.
How about this?
comboBoxMunicipality.SelectedValue = theMunicipalityIDtoSelect
I have a ComboBox with the values "open", "closed". According to values changed in the ComboBox, I want to change the DataGrid to display either "open " or "closed" values. How can I do this?
Your DataGrid can be bound to a DataView. Create different DataViews depending upon the Selected item in the DropDownList (here's an article on how to retrieve the selected item).
Fill a DataTable with your data. Derive different DataViews for the combo states. When the combo changes (enable AutoPostback), select the appropriate DataView and Bind the DataGrid.
You could use RowFilter property of a DataView object binded to the DataGrid.
You can use any of the *DataSource controls and add a control parameter with the ID of the combobox. Turn on autopostback for the combo, and asp.net will automatically call the object datasource with the new open/closed value.