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

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.

Related

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

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";

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?

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

combo box selected index changed all comboboxes are changed

I have two combo boxes in my form. i have binded these two with different database tables but with the same display member and some of data are repeated. When i change the index of first combo box ,the second combo box index also changed.
how to clear this problem. please any one give me the solution
Thanks
//dt1---> a b c d
//dt2---> x c a y
cmb_STable.SelectedIndex = -1;
cmb_STable.DisplayMember = "tablename";
cmb_STable.DataSource = dt1;
cmb_mTable.SelectedIndex = -1;
cmb_mTable.DisplayMember = "tablename";
cmb_mTable.DataSource = dt2;
// if cmb_mTable_SelectedIndexChanged with a
, the cmb_sTable also changed with a
Shot in the dark:
I assume you are using ADO.Net to databind the comboboxes. Depending on the API you will either have a bindingsource or a CurrencyManager in the background which has a copy of the databound list and a pointer to the current item.
My guess is that you have bound both lists to this datasource via the same CurrencyManager/BindingSource and moving it on one control moves it on the other.
You need to add a new BindingSource or currencymanager to stop this behaviour.
Your code still doesn't show everything.
It looks like there is a Databinding for the SelectedValue for both boxes. If they are bound to the same item (copy pasted?) , then that explains your problem.

c# wpf checkbox content from one list to another

I have a list where i dynamically store checkboxes(with grid content in it). Next to the list i have a button who,when clicked, must put the selected checkboxes with content in the right list. When delete from right list is clicked, the selected item(s) on the right side needs to be deleted.
How is this possible? Suppose i have this code:
CheckBox cbox = new CheckBox();
Grid panel= new Grid();
panel.Width = 260;
cbox.Content = panel;
You can use:
Listbox.ItemTemplate to represent the data you want (so that it looks like CheckBox:Label)
Use 2 ObservableCollections to store 2 lists that you have
In your buttons you can bind to Current Item in the List it is associated with
On button click, it will get current select Item and Add/Remove from ObservableCollections as needed
Let me know if you need specific details. The good thing about ObservableCollections is that whenever collection changes, anything bound to it (like ListBox) will get updated.
so that any manupilation with the data is outside View so to speak and done on ViewModel.

Categories

Resources