Groups | Blog | Home
all groups > dotnet windows forms databinding > february 2006 >

dotnet windows forms databinding : BindingSource - Nested Properties


Chris
2/19/2006 11:32:51 PM
Hi @all.

I have a problem with the use of a BindingSource in connection with
nested properties (relations).

Let´s imagine the following situation:

class Person {

public ICollection<Position> Positions {
...
}
....
}

class Position {

public string Description {
...
}

public Person Person {
...
}

public Person Company {
...
}
....
}

class Company {
public string Name {
...
}
...
}




Person ... a person
Position ... a position of a person in a company
Company ... a company

I want to display/edit Persons on a form. So I use a BindingSource with
"typeof(Person". Thats works fine. In a grid (on the "person-form") I
want to display (and edit) the positions of a person. So I bind the
DataSource-Property of the Grid to "personBindingSource.Positions".
When I add Columns to the grid I can bind to "Description" but not to
nested Properties of "Position" - I want to display the name of the
Company. I would like to bind to "Company.Name", but that does not work.


Gicks
3/18/2006 12:44:23 AM
you can use :

yourDataGridView.VirtualMode = true;

then the CellValueNeeded event like this :

private void myDataGridView_CellValueNeeded(object sender,
DataGridViewCellValueEventArgs e)
{
if (myDataGridView_.Rows[e.RowIndex].IsNewRow) return;

Person person = (Person
)myDataGridView.Rows[e.RowIndex].DataBoundItem;
e.Value = person.Company.Name;

}


Chris a écrit :
[quoted text, click to view]
Gicks
3/18/2006 12:48:23 AM
Forgot to test the column name :

private void myDataGridView_CellValueNeeded(object sender,
DataGridViewCellValueEventArgs e)
{
if (myDataGridView.Rows[e.RowIndex].IsNewRow) return;
if (myDataGridView..Columns[e.ColumnIndex].Name != "CompanyName") return;

Person person = (Person
)myDataGridView.Rows[e.RowIndex].DataBoundItem;
e.Value = person.Company.Name;

}

Gicks a écrit :
[quoted text, click to view]
AddThis Social Bookmark Button