I am doing simple WinForms application, and I am facing some strange problem.
My form:
It is as easy as it can be: 3 comboboxes, and two buttons - OK and Cancel.
View:
private void applyOrderButton_Click(object sender, EventArgs e)
{
List<string> testList = new List<string>()
{
"A",
"B",
"C"
};
comboBox1st.DataSource = testList;
comboBox2nd.DataSource = testList;
comboBox3rd.DataSource = testList;
comboBox1st.SelectedIndex = 2;
comboBox2nd.SelectedIndex = 1;
comboBox3rd.SelectedIndex = 0;
//Presenter.DoTest();
}
What happens after caling method applyOrderButton_Click() (it happens after Ok button is clicked) all of my comboBoxes change selected position. However, each of those comboBoxes have the same selected index - in this particular case it will be "A".
Then I change change comboBox selectedIndex using my cursour (for example I choose 3rd comboBox to show "C") the change is performed for all three comboBoxes. What I am doing wrong?
You are running across something that is present in the background of WinForms called the "CurrencyManager".
Its job is to synchronize the "current record" across all bindable controls that refer to the same data source.
For instance, if you had added a label and bound it to the same list, and bound it so that it shows a property of one of the objects, it would always show the property value of the same object you had selected in the combobox.
One bonus of this is that you could easily add a form that edits a list of objects, binding textboxes and such to the properties of one of the objects and adding a navigator that allows you to move to the next or previous row. You would not have to manually ensure all textboxes refer to the correct object, the CurrencyManager would do all this for you.
However, in your case, since you bound the same data source to all three comboboxes, the CurrencyManager will ensure all three select the same row. If you select a new row in one of the comboboxes, the CurrencyManager will go and fix all the others to refer to the same row.
You can fix this in various ways:
You can override the binding context for each combobox:
comboBox2nd.BindingContext = new BindingContext();
comboBox3rd.BindingContext = new BindingContext();
Note that if you go this route you need to do this before assigning the SelectedIndex or SelectedItem properties, otherwise the CurrencyManager will have updated the other two comboboxes before you assigned new BindingContexts.
You can assign distinct data sources to each combobox:
combobox2nd.DataSource = testList.ToList();
combobox3rd.DataSource = testList.ToList();
Just assign new context to other ComboBoxes like that:
List<string> testList = new List<string>()
{
"A",
"B",
"C"
};
comboBox1st.DataSource = testList;
comboBox2nd.BindingContext = new BindingContext();
comboBox2nd.DataSource = testList;
comboBox3rd.BindingContext = new BindingContext();
comboBox3rd.DataSource = testList;
comboBox1st.SelectedIndex = 2;
comboBox2nd.SelectedIndex = 1;
comboBox3rd.SelectedIndex = 0;
The CurrencyManager is used to keep data-bound controls synchronized with each other (showing data from the same record). The CurrencyManager object does this by managing a collection of the bound data supplied by a data source. For each data source associated with a Windows Form, the form maintains at least one CurrencyManager. Because there may be more than one data source associated with a form, the BindingContext object manages all of the CurrencyManager objects for any particular form. More broadly, all container controls have at least one BindingContext object to manage their CurrencyManagers.
Related
been searching for a solution for this problem, found plenty solutions, but nothing changed my code's behaviour. This is in WinForms.
I am loading a form with a ComboBox, that contains the values and names of this enum, that is inside a class named "Node".
class Node
{
public enum NodeType { Yield, Home, Parking, Light, None, Inbound, Outbound }
public NodeType Type;
}
Then in my form, I have a ComboBox named "Type", which is set up like so (from the constructor):
Node node = new Node();
node.Type = Node.NodeType.Home;
Type = new ComboBox();
Type.Location = new Point(77, 41);
Type.Size = new Size(121, 24);
Type.DropDownStyle = ComboBoxStyle.DropDownList;
Type.DisplayMember = "Name";
Type.ValueMember = "Value";
Type.DataSource = Enum.GetValues(typeof(Node.NodeType));
Type.SelectedValue = node.Type;
Controls.Add(Type);
When the program runs, the list shows all the names, and on closing the form I am able to retrieve the selected value via. Type.SelectedValue. My problem is that the ComboBox doesn't start at the value that the Node is already set at. Essentially the line
Type.SelectedValue = node.Type;
doesn't do anything. I've tried using SelectedItem which didn't change anything, and
Type.SelectedIndex = (int)node.Type;
Which caused an ArgumentOutOfRangeException.
So, my question is: how do I set the start value of the ComboBox?
There are several mistakes in that code.
First, enum does not have Name and Value properties (in fact it does not have any property), so DisplayMember and ValueMember cannot be used and should be left blank (default). Which in turn means SelectedValue cannot be used and you need to use SelectedItem instead.
Second, you are using list data bound mode for the list portion of your ComboBox by setting DataSource property instead of populating Items, which is fine, but data binding occurs later in the process, so inside the constructor the Items property is empty and SelectedItem has no effect. In order to fix that, you need to move the data initialization part to your form Load event.
So, in your form constructor you'll have this:
Type = new ComboBox();
Type.Location = new Point(77, 41);
Type.Size = new Size(121, 24);
Type.DropDownStyle = ComboBoxStyle.DropDownList;
Controls.Add(Type);
and in your form Load event - this:
Node node = new Node();
node.Type = Node.NodeType.Home;
Type.DataSource = Enum.GetValues(typeof(Node.NodeType));
Type.SelectedItem = node.Type;
Type.SelectedIndex = index from your enum, for example the enum is {"apple", "pear", "pineapple"}, and you want pear by deafult, so Type.SelectedIndex = 1
I'm trying to find a way to add data from one datagrid to another and for that data to be inserted to only one column at a time in my second datagrid. The specific column is created each time the Add button has been clicked.
My coding so far:
private void btnFeedbackAddSupplier_Click(object sender, RoutedEventArgs e)
{
dgFeedbackSelectSupplier.Items.Clear(); //So that my rows do not stack each other on every add
DataGridTextColumn columnSupplier = new DataGridTextColumn();
columnSupplier.Binding = new Binding("Supplier");
DataGridTextColumn columnFeedbackSupplierItem = new DataGridTextColumn();
//The 'Item' column is binded in XAML
columnSupplier.Header = (cmbFeedbackSelectSupplier.SelectedItem as DisplayItems).Name;
columnSupplier.IsReadOnly = true;
dgFeedbackAddCost.SelectAll(); //Selects all the rows in 1st datagrid
//Casts selected rows to my 'ViewQuoteItemList' class
IList list = dgFeedbackAddCost.SelectedItems as IList;
IEnumerable<ViewQuoteItemList> items = list.Cast<ViewQuoteItemList>();
var collection = (from i in items let a = new ViewQuoteItemList { Item = i.Item, Supplier = i.Cost }
select a).ToList();
//Adds both the column and data to the 2nd datagrid
dgFeedbackSelectSupplier.Columns.Add(columnSupplier);
foreach (var item in collection)
dgFeedbackSelectSupplier.Items.Add(item);
}
My reason for wanting to add the data to only one separate column at a time is because the data is different each time I want to add it to my 2nd datagrid and it overwrites any previous data that was entered in older add's.
EDIT: I Here are some images of what my current problem is
Here I add the first company and it's values. Everything works fine
Here I add the second company with it's new values, but it changes the values entered with the first company. This is my big problem. So you can see how my values are changed from the first to the second image
I think your problem here is that all your columns are bound to the same property: Supplier. Since you're updating that property everytime, all columns are assigned the same value. In the end, there's only one Supplier property for each row, so you can't show a different value for that single property on each column since everytime you change that property's value, the Bindings get notified and update themselves.
Maybe you could try using a OneTime Binding instead of a regular one. That way, the cells would retain the value they had when you first added them to the DataGrid. But for that to work, you should avoid clearing the DataGrid's items list, since re-adding the items would force them to rebind again.
Another option would be having a list of suppliers in your Supplier property, and have each column bind to an index of that list.
private void btnFeedbackAddSupplier_Click(object sender, RoutedEventArgs e)
{
// ...
columnSupplier.Binding = new Binding(string.Format("Supplier[{0}]", supplierColumnIndex));
// ...
var supplierCosts = new List<int>();
// ...
// Fill the list with the Costs of the Suppliers that correspond to each column and item
// ...
var collection = (from i in items let a = new ViewQuoteItemList { Item = i.Item, Supplier = supplierCosts }
select a).ToList();
//Adds both the column and data to the 2nd datagrid
dgFeedbackSelectSupplier.Columns.Add(columnSupplier);
foreach (var item in collection)
dgFeedbackSelectSupplier.Items.Add(item);
}
I'm having an issue when I select an item from a DataGridComboBoxColumn. The cell wont display the name of the item I've chosen after I move focus to the next cell. This is the code I have:
DataGridComboBoxColumn cb1 = new DataGridComboBoxColumn();
cb1.ItemsSource = listOStrings;
cb1.TextBinding = new Binding("listOfStrings");
e.Column = cb1;
e.Column.Header = "SomeTitle";
Where listOfStrings is a list that is being updated by the user. I have another DataGridComboBoxColumn that has its ItemSource set to a list of strings that isn't being updated. That one displays the text just fine, although the code for the two is the same. I'm wondering why my cb1 combo box wont display the values after leaving the cell but the other one does?
When a binding in WPF is hooked up to a non-static source, the underlying source needs to implement iNotifyPropertyChanged. In your case you may want to use an ObservableCollection as answered here: Why does a string INotifyPropertyChanged property update but not a List<string>?
In your case, it would look something like:
private ObservableCollection<string> _listOStrings = new ObservableCollection<string>();
public ObservableCollection<string> ListOStrings
{
get
{
return _listOStrings;
}
set
{
_listOStrings = value;
OnPropertyChanged("ListOStrings");
}
}
For more information on iNotifyPropertyChanged from MSDN, see:
See: https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx
I've never done binding the way you are doing it - have you considered moving the UI to the XAML and databinding to a ViewModel? Here is an awesome step by step example on databinding comboboxes. You would just have the combobox be a column within the DataGrid also - similar to this.
I have a gridControl who's data source is a List.
Each item in List is made out of three fields.
And I have 3 columns in the gridControl.
when I programatically insert values into the gridview it does not appear.
Here is my code.
public Company_Selection()
{
InitializeComponent();
companies = new List<DataLibrary.Companies>();
gridControl1.DataSource = companies;
}
internal void load_all_companies(List<DataLibrary.Companies> other)
{
for (int i = 0; i < other.Count; i++)
{
companies.Add(other[i]);
gridView1.SetRowCellValue( i, "Id", other[i].id);
gridView1.SetRowCellValue( i, "Company", other[i].name);
gridView1.SetRowCellValue(i, "Description", other[i].description);
gridView1.RefreshData();
gridControl1.RefreshDataSource();
}
}
Any ideas about what's wrong ?
The thing is you should not work with cells directly. Your 'companies' list should be enclosed in BindingList. All programmatical changes are done at object, not grid level. So, setting datasource becomes
gridControl1.DataSource = new BindingList<DataLibrary.Companies>(companies);
This will take care of presentation, changes, additions and deletions of objects within list. The columns will be created automatically if your gridView does not contain any and AutoPopulateColumns is true. You might want to setup columns in gridView using Designer. Don't forget so set Field property to Property name of underlying object.
You correcly set the datasource property, but you have also to bind the datasource to the control by using the DataBind method like this
gridControl1.DataSource = companies;
gridControl1.DataBind();
I think you for get to write DataBind method
gridControl1.DataBind();
so you code will be
companies = new List<DataLibrary.Companies>();
gridControl1.DataSource = companies;
gridControl1.DataBind();
GridView.DataBind Method
: Binds the data source to the GridView control.
Use the DataBind method to bind data from a data source to the GridView control. This method resolves all data-binding expressions in the active template of the control.
I believe the cause of the issue is that you are using fieldnames like "Id","Company", "Description" but the grid is mapped on "id","name","description" fieldnames:
gridView1.SetRowCellValue( i, "Id", other[i].id);
gridView1.SetRowCellValue( i, "Company", other[i].name);
gridView1.SetRowCellValue(i, "Description", other[i].description);
Also passing row cell values directly to a view and refreshing the grid after appending every record is very redundant. The single RefreshDataSource method call after data loading is only enough:
internal void load_all_companies(List<DataLibrary.Companies> other) {
for(int i = 0; i < other.Count; i++)
companies.Add(other[i]);
gridControl1.RefreshDataSource();
}
This may have been already asked but I can't seem to find this specific question, so here goes...
I have a form in C# where every textbox is bound to a field in a row. Rows can be cycled through by some buttons on the bottom but all the data displayed at a time in the from is from one row. Any changes that are made get updated back to the database when the user clicks "update"
One field (class) is an enumeration (0,1,2) where only the value is stored in the database, but doesn't mean much to the user. I was asked to make this more obvious to the user, so I decided to go with a dropdown style combo box. Since the database didn't have any reference to what the values meant, I decided to use the DataBindings instead of DataSource so I could just use the index as the data bind, but it seems that SelectedItem or Value are not the way to do this.
Here is my goal:
1 exists in database, so "B" is selected in combo box.
User selects "C" and updates the database, 2 is now stored in the database.
Any thoughts on what I need to get this working?
I assume you have a BindingSource on your form to bind to the data. You can bind the SelectedIndex property of the ComboBox as follows:
comboBox.DataBindings.Add("SelectedIndex", bindingSource, "PropertyInTheDataSource");
Actually I was able to bind it to a custom Object. It's a little too much work for such a simple task. But you have complete control on Display/Value pairs. Anyway, I thought I'd share and you decide:
Create a new class (say CustomItem) with 2 fields:
Public int Value{get;set;}
public string Title {get;set;}
Then in you form:
var item1 = new CustomItem() { Title = "A", Value = 10 };
var item2 = new CustomItem() { Title = "B", Value = 20 };
var item3 = new CustomItem() { Title = "C", Value = 30 };
var lst = new List<CustomItem>();
lst.Add(item1);
lst.Add(item2);
lst.Add(item3);
comboBox1.DataSource = lst;
comboBox1.DisplayMember = "Title";
comboBox1.ValueMember = "Value";
Now You have a databound combobox in case you don't have BndingSource in your form.
Just remember to define your class's Title and Value as properties otherwise it wouldn't work.