Reputation: 450
I am experimenting and learning windows forms application and I am unable to figure out the binding construct. As per the MSDN documentation:- You can specify an instance of any of the following classes for the data source:
DataSet
DataTable
DataView
DataViewManager
BindingSource
So, I have a simple code to understand this construct.
I have have initialized a simple windows form with listbox listBox1 and DataTable data_table.
static DataTable data_table = new DataTable("ParentTable");
DataColumn column;
DataRow row;
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
column.ReadOnly = false;
column.Unique = true;
// Add the Column to the DataColumnCollection.
data_table.Columns.Add(column);
for (int i = 0; i <= 2; i++)
{
row = data_table.NewRow();
row["id"] = i;
data_table.Rows.Add(row);
}
can directly bind to DataSource by.
listBox1.DataSource=new BindingSource(data_table , null);
listBox1.ValueMemeber="id";
But when I use the following
Binding myBinding = new Binding("DataSource", data_table, "id");
form_m.listBox1.DataBindings.Add(myBinding);
I get exception
System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource
I will really appreciate if someone can help me with this.
Upvotes: 1
Views: 2007
Reputation: 32443
You are using constructor overload with three arguments Binding(String, Object, String)
Where parameters are
propertyName
- The name of the control property to bind.
- DataSource
in your case
dataSource
- An Object that represents the data source.
- This is a main datasource, notice it is not a datasource which will be bounded to List.Box.DataSource
dataMember
- The property or list to bind to.
- This is a name of the property in the main datasource, which value will be bound to the control's DataSource
. This value should be an IList or an IListSource
You can set ListBox.DataSource
without any extra binding
listBox.DataSource = myDataTable;
Check the link: Windows Forms Data Binding
There are should be enough information about how you can use data binding and if you go through examples you will get pretty good picture of how you can use data binding in for your requirements.
Upvotes: 1
Reputation: 1136
You don't need any extra binding. You can assign List, Array, Tables to the DataSource and also provide DisplayMember and ValueMember while you use List of object or table as DataSource if you use single dimension array then you don't need to provide DisplayMember and ValueMember.
listbox1.DataSource = data_table;
listbox1.ValueMember = "id";
listbox1.DisplayMember = "id";
Upvotes: 0