Change DataSource in XtraGrid - c#

i have two different Lists.
List<MyClass> list1 = new List<MyClass>();
List<OtherClass> list2 = new List<OtherClass>();
I bind one of them to a xtragrid form devexpress:
gridControl.DataSource = list1;
That works. Now i want to change the list but the columns doesn't change.
Can someone help me?

Call gridView.PopulateColumns().

Related

Binding List to several ComboBoxes

I have a windows form with many comboboxes. They all have to display the same items and I want to be able to remove items from their list of values. So I decided to try and make a List variable so I could easily remove and insert values into it. So what I did was
List<string> Ranks = new List<string>(new string[] { "values here" });
Then in my Form1Designer.cs
this.ComboBox_Rank_0.DataSource = Ranks;
I heart ValueMember and DisplayMember were good things to have, but so far it works without them. When compiled the form loads and the comboboxes have the correct values.
The problem is that when I choose a value in one combobox the others get the same value selected, as well. Any ideas?
Use a BindingList<T>
This class allows you to handle the interaction with your list separately for each of the combos
For example
List<string> names = new List<string>()
{"Steve", "Mark", "Luke", "John", "Robert"};
BindingList<string> bl1 = new BindingList<string>(names);
ComboBox_Rank_0.DataSource = bl1;
BindingList<string> bl2 = new BindingList<string>(names);
ComboBox_Rank_1.DataSource = bl2;
The BindingList<T> requires using System.ComponentModel; and notice that you don't require the new string[] syntax in the constructor of your list

Winform Combobox with DataSource as List<int>

This may be a very simple question but I realized I could not get it work.
I have a Winform combo box with datasource as a List<int>
combo.DataSource = intList;
What do I put for .DisplayMember and .ValueMember in order to simply have a list of integer values? Not setting them will display nothing.
I have worked with other List<myObj> in which DisplayMember and ValueMember are myObj's properties. How about simple data type like int, string?
When retrieving the selected item, one could simply cast (int)(combo.SelectedItem) or have to go through the property corresponding to ValueMember?
The problem does not occur because you have a list of integers, it probably occurs because you add items to the list after assigning it to the .DataSource property. List does not have a mechanism to notify its container when items are added to or removed from it.
Either add items to the list before assigning it to the .DataSource property, or use a wrapper like BindingSource as Krishnraj Rana suggested.
Here BindingSource comes into picture. You can use it like this.
BindingSource bSource = new BindingSource();
bSource.DataSource = new List<int> { 1, 2, 3 };
combo.DataSource = bSource;
Though you can set datasource of combobox directly with list. like this -
combo.DataSource = intList;
This also works perfectly fine.
You can add items from list using foreach like this.
foreach (var v in intList)
{
comboBox1.Items.Add(v.ToString());
}

Combobox Datasource not creating items

After much searching I have still not found a solution to this.
I have created a list in VS 2010 and bound it to a ComboBox. The DataSource property shows the entire list but the combobox on the form is empty, as is the items property of the box.
private List<string> classes = new List<string>();
private BindingList<string> bindingClasses;
classes.Add("Spinning");
classes.Add("Step");
classes.Add("Pilates");
classes.Add("Kickboxing");
classes.Add("Body Sculpting");
bindingClasses = new BindingList<string>(classes);
classesComboBox.DataSource = bindingClasses;
I have also tried adding
classesComboBox.DisplayMember = "Spinning";
and without the bindingList between to no avail.
Why is this not working?
Lose the BindingList, just set the DataSource to the List<String> itself.
List<string> classes = new List<string>();
classes.Add("Spinning");
classes.Add("Step");
classes.Add("Pilates");
classes.Add("Kickboxing");
classes.Add("Body Sculpting");
classesComboBox.DataSource = classes;
classesComboBox.SelectedItem = "Spinning";
classesComboBox.DataBind();
You need to specify when it should grab the data.

Binding Listbox to List<object> in WinForms

What's the simplest way to bind a Listbox to a List of objects in Windows Forms?
You're looking for the DataSource property:
List<SomeType> someList = ...;
myListBox.DataSource = someList;
You should also set the DisplayMember property to the name of a property in the object that you want the listbox to display. If you don't, it will call ToString().
Binding a System.Windows.Forms.Listbox Control to a list of objects (here of type dynamic)
List<dynamic> dynList = new List<dynamic>() {
new {Id = 1, Name = "Elevator", Company="Vertical Pop" },
new {Id = 2, Name = "Stairs", Company="Fitness" }
};
listBox.DataSource = dynList;
listBox.DisplayMember = "Name";
listBox.ValueMember = "Id";
Pretending you are displaying a list of customer objects with "customerName" and "customerId" properties:
listBox.DataSource = customerListObject;
listBox.DataTextField = "customerName";
listBox.DataValueField = "customerId";
listBox.DataBind();
Edit: I know this works in asp.net - if you are doing a winforms app, it should be pretty similar (I hope...)
Granted, this isn't going to provide you anything truly meaningful unless the objects have properly overriden ToString() (or you're not really working with a generic list of objects and can bind to specific fields):
List<object> objList = new List<object>();
// Fill the list
someListBox.DataSource = objList;
ListBox1.DataSource = CreateDataSource();
ListBox1.DataTextField = "FieldProperty";
ListBox1.DataValueField = "ValueProperty";
Please refer to this article for detailed examples.
I haven 't seen it here so i post it because for me is the best way in winforms:
List<object> objList = new List<object>();
listBox.DataSource = objList ;
listBox.Refresh();
listBox.Update();
There are two main routes here:
1: listBox1.DataSource = yourList;
Do any manipulation (Add/Delete) to yourList and Rebind.
Set DisplayMember and ValueMember to control what is shown.
2: listBox1.Items.AddRange(yourList.ToArray());
(or use a for-loop to do Items.Add(...))
You can control Display by overloading ToString() of the list objects or by implementing the listBox1.Format event.
For a UWP app:
XAML
<ListBox x:Name="List" DisplayMemberPath="Source" ItemsSource="{x:Bind Results}"/>
C#
public ObservableCollection<Type> Results

bind datagrid to collection

how do I bind a DataGrid to a collection ? (e.g : List ).
i used this :
Datagrid dg = new Datagrid();
dg.DataSource = myCollection;
dg.DataBind();
but after this, the Datagrid still doesn't display the data from the collection.
The same way as you bind a datagrid to a DataTable/DataSet. Your object properties will behave like column names when databinding.
DataGrid1.DataSource = myList;
DataGrid1.DataBind();
List<string> lst= new List<string>();
lst.Add("your string");
Datagrid dg = new Datagrid();
dg.DataSource=lst;
dg.DataBind();
disclaimer: I have not run this code, but this should give your a general idea
You have to add your Datagrid object (dg) to your form.
this.Controls.Add(dg);
Just set YourDataGrid.Datasource to the instance name of the List and call
the YourDataGrid.DataBind() function.

Categories