Groups | Blog | Home
all groups > dotnet windows forms databinding > december 2005 >

dotnet windows forms databinding : Binding many controls to same DataSource using different Position


Rob van Gelder
12/19/2005 12:17:02 PM
Hi.

I'm fairly new at C# and .NET Framework. I'd really appreciate your advice.
I'm using 2.0.

I would like to have many read-only controls on my form, each using a
different portion of data from my data source.

I have a Class (called MyClass) containing a Property named DisplayValue.
DisplayValue is what should be bound for all of those controls.

My approach so far has been to create MyClassList : BindingList<MyClass>
Code extract as follows:

MyClassList myclasses;
....
myclasses = new MyClassList();

for (int i=0; i<10; i++)
{
MyClass temp = new MyClass();
temp.DisplayValue = "Record " + i.ToString();
myclasses.Add(temp);
}
BindingSource binding;

binding = new BindingSource();
binding.DataSource = myclasses;
textBox1.DataBindings.Add(new Binding("Text", binding,
"DisplayValue"));
binding.Position = 5;

binding = new BindingSource();
binding.DataSource = myclasses;
textBox2.DataBindings.Add(new Binding("Text", binding,
"DisplayValue"));
binding.Position = 6;
....

class MyClassList : BindingList<MyClass> {}

class MyClass
{
private string _myvalue;

public string DisplayValue
{
get { return _myvalue; }
set { _myvalue = value; }
}

public MyClass() {}
}

In the end my form will contain about 400 controls, so I'm looking at the
repeated creation of BindingSource and wondering if there is a cleaner way of
achieving the same.

I'm open to suggestions.

Regards,
Bart Mermuys
12/20/2005 5:02:54 PM
Hi,

[quoted text, click to view]

Not really, the problem is that you're using rows as columns which is why
you need to use different BindingSource's.

Depending on what you exactly want, you could consider using a single object
that has dynamic properties ( ICustomTypeDescriptor/PropertyDescriptor).

Ussually you would bind a list to a list control (eg. DataGrid(View)).

HTH,
Greetings

[quoted text, click to view]

AddThis Social Bookmark Button