How do I rebind a combobox in winforms?[duplicated] - c#

I have a Winforms application and a combobox has it's datasource set to a List when the form loads. The data displays fine in the combobox.
Then after a user clicks a button I want to create a new List and assign that List as the datasource for the combobox.
The problem is that after setting the datasource to be the new List the items in the combobox don't change. Here is the code I'm using.
var newPersonList=new List<Person>(){//...};//Person has a property named 'Name'
dlCustomer.DataSource = newPersonsList
dlCustomer.DisplayMember = "Name"
dlCustomer.Refresh()
Does anyone know how to make the correct data be displayed in the combobox the second time I assign new data source for it?

You can clear the items and then set the DataSource as below
dlCustomer.Items.Clear();
dlCustomer.DataSource = newPersonsList;
dlCustomer.DisplayMember = "Name";

Related

C# Trying to find an unbound binding source [duplicate]

I am creating a ComboBox array dynamically and the DataSource for all the ComboBox is a single integer list that contains some integers. But when I change a value say X in any one combo box then all other combo values get reset to value X.
So here is the situation:
All combo box controls are bound to a single list
When I change selected item of a combo box, selected item of all other combo box controls also change.
How can I stop these behavior?
Since you are binding all combo boxes to the same data source - a single list - they are using a single BindingManagerBase.
So when you choose an item from one of combo boxes, the current Position of the shared binding manager base changes and all combo boxes goes to that position of their shared data source.
To solve the problem you can bind them to different data source:
You can bind them to yourList.ToList() or any other list for example different BindingList<T>.
combo1.DataSource = yourList.ToList();
combo2.DataSource = yourList.ToList();
You can use different BindingSource for them and set your list as DataSource of BindingSource
combo1.DataSource = new BindingSource { DataSource= yourList};
combo2.DataSource = new BindingSource { DataSource= yourList};
Also as another option:
You can use different BindingContext for your combo boxes. This way even when you bind them to a single list, they are not sync anymore.
combo1.BindingContext = new BindingContext();
combo1.DataSource = yourList;
combo2.BindingContext = new BindingContext();
combo2.DataSource = yourList;
In fact all controls of the form use a shared BindingContext. When you bind 2 controls to a same data source, then they also use the same BindingManagerBase this way, when you for example move to next record, all controls move to next record an show value from bound property of next record. This is the same behavior that you are seeing from your combo boxes. Being sync for controls which are using the same BindingManagerBase is a desired behavior. Anyway sometimes we don't need such behavior. The post shares the reason and the solution.

BindingSource And Composite Properties

I have a method that I call in a form that returns a List and then I throw that into a BindingSource and throw the BindingSource into a DataGridView. The Order class has a property called Items that contains a List. My Order and Item class both implement the INotifyPropertyChanged Interface.
What I'm trying to do is that when a row is clicked in the DataGridView it pulls up the list of items associated with the order from it's Items property into another grid. I want to ensure that those items are 2 way binded to the grid but I can't call it the way I'd like to...
itemsGrid.DataSource = orderBindingSource[orderGrid.CurrentRow.Index].Items;
I can't access the properties of the list from within the BindingSource (After the . operator, Items is not an available option)... Any recommendations? I've tried using a BindingList<Order> and BindingList<List<Order>> but was getting implicit conversion errors. Any suggestions?
*EDIT: Ok I've managed to get the BindingList to work. I didn't know I had to add the list like this
orderList = new BindingList<Order>(Method To Retrieve List<Order>)
Aside from this, the first grid displaying the orders works and updates perfectly. The sub grid which contains the Order's Items bound like
itemsGrid.Datasource = orderList[ordersGrid.CurrentRow.Index].Items;
It displays properly. I don't have editing enabled as I have a few textboxes that are setup and each textbox is bound to the Item from the orderList of the currently selected row of the ordersGrid. I can update the data in the textboxes for the selected item in the subgrid but the changes when I leave the textbox, the sub grid's row for that item property edited does not update as I leave the textbox. It will, however, update if I select another item in the grid. How can I get it to update as I leave the textbox instead of having to click on another row in the grid?

Bind a List from a Database to a ComboBox Controls collection?

I can apply the first value from the database to the text of the combo box control like this:
Entities context = new Entities();
comboBox1.DataBindings.Add(new Binding
("Text", context.table1, "column1", true));
But I would like to apply the results to a collection or list for the control to bind to and then all results will be returned when the drop down button is clicked. What property do I need to target? "SelectedValue" was not working for me...
Maybe comboBox1.DataSource ?

Binding the combobox in C# desktop application

I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and name of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource of the combobox to your datasource. Then the ValueMember for the Roll No and the DisplayMember for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"

Dynamically Filter data in a DataGrid

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.

Categories

Resources