Conditions of DataBinding

M

Martin

what are the conditions in order to bind to an object? i have an object,
which implements the IList interface. Now I'am able to bind the objects,
which are hold by the collection.

now i implemented two properties in my collection. but i'm not able to bind
to the properties of the collection itself. it must be the object in the
list. that are my experiences.

Example:

public class MyCollection : IList {

//Implementations of IList and so on..

public event EventHandler myValueChanged;

public int myValue
{
get { return iMyValue; }
set
{
iMyValue = value;

if(myValueChanged != null)
myValueChanged(this, EventArgs.Empty);
}
}
}

And i try to bind to an object..

//That works...
textBox1.DataBindings.Add("Text", myClassInstance[1],
"ValueOfMyObjectInList");

//That doesn't work
numericBox1.DataBindings.Add("Value", myClassInstance, "myValue");

I get an error which tells me, that he isn't able to bind to a property or
column of the datasource.
parameter name: dataMember

is it possible to bind to a property of a collection itself?
 
B

Bart Mermuys

Hi,

Martin said:
what are the conditions in order to bind to an object? i have an object,
which implements the IList interface. Now I'am able to bind the objects,
which are hold by the collection.

now i implemented two properties in my collection. but i'm not able to
bind to the properties of the collection itself. it must be the object in
the list. that are my experiences.

Example:

public class MyCollection : IList {

//Implementations of IList and so on..

public event EventHandler myValueChanged;

public int myValue
{
get { return iMyValue; }
set
{
iMyValue = value;

if(myValueChanged != null)
myValueChanged(this, EventArgs.Empty);
}
}
}

And i try to bind to an object..

//That works...
textBox1.DataBindings.Add("Text", myClassInstance[1],
"ValueOfMyObjectInList");

//That doesn't work
numericBox1.DataBindings.Add("Value", myClassInstance, "myValue");

I get an error which tells me, that he isn't able to bind to a property or
column of the datasource.
parameter name: dataMember

is it possible to bind to a property of a collection itself?

No. The fields come from the property on the items, but so does the
values. If you databind a list then there is a CurrencyManager that
maintaince the current position and current values.

You could put your properties outside of the collection into another class
together with an instance of the custom collection.

public class MyComplexObject
{
private int iMyValue;
private MyCollection myCollection = new MyCollection();

// collection property
public MyCollection Collection
{
get { return myCollection(); }
}

// properties on the same level as the collection
// if necesairy these properties could access MyCollection
public event EventHandler myValueChanged;
public int myValue
{
get { return iMyValue; }
set
{
iMyValue = value;
if(myValueChanged != null)
myValueChanged(this, EventArgs.Empty);
}
}
}

public class MyCustomCollection : BindingList<MyCustomObject>
{
// regular typed methods
}

public class MyCustomObject
{
// item properties
}

Then you can bind like:
MyComplexObject myObj = new MyComplexObject();
TextBox.DataBindings.Add("Text", myObj, "myValue" );
DataGrid.DataSource = myObj;
DataGrid.DataMember = "Collection";


HTH,
Greetings
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top