Deep object binding

  • Thread starter Thread starter Benny
  • Start date Start date
B

Benny

I just cant find a good explanation on how to "deep bind" objects, so
im hoping someone can help. I have an object that contains another
object, for example: Book has a title, pagecount, and an Author. The
title and pagecount are system objects but Author is a type i have
definied. I need to know how i could bind the Author.FirstName
property (for example) to the same grid like any property of the
containing object. another example:

datagrid1.DataSource = Books1;
....
datacolumnstyle1.MappingName = "Title";
....
datacolumnstyle2.MappingName = "Author.FirstName";
....

There are a couple articles out there for doing this but there is no
explanation on how to customize the code and im brand new to
reflection. Any help would be greatly appreciated.
 
That one is simple, actually binding the object is slightly more tricky (you
need drop down lists and binding to "SelectedItem") but this just binding the
odd property (e.g. author name) requires a little composition like so.

Try this.

class MyBookWrapper()
{
private Book book;

MyBookWrapper(Book book)
{
this.book = book;
}

public string AuthorName
{
get{return book.Author.Name;}
set{book.Author.Name = value;}
}
}

HTH.
 
I should have added that you bind the wrapper to the control, instead of the
original object.
 
Benny said:
I just cant find a good explanation on how to "deep bind" objects, so
im hoping someone can help. I have an object that contains another
object, for example: Book has a title, pagecount, and an Author. The
title and pagecount are system objects but Author is a type i have
definied. I need to know how i could bind the Author.FirstName
property (for example) to the same grid like any property of the
containing object. another example:

datagrid1.DataSource = Books1;
...
datacolumnstyle1.MappingName = "Title";
...
datacolumnstyle2.MappingName = "Author.FirstName";
...

There are a couple articles out there for doing this but there is no
explanation on how to customize the code and im brand new to
reflection. Any help would be greatly appreciated.

Are you saying that "Author.FirstName" doesn't work? I don't use datagrids
but, we do deep binding to other controls.

Here's how you would bind the Authors first name to a text control:

controlName.DataBindings.Add("Text", bindingSource,
"Author.FirstName");

In our code, "bindingSource" would be a BookCollection which contains a
collection of Books.
 

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

Back
Top