C
connected
I'm having difficulty with populating a DataGridView control with data
correctly. It works with a single class, for example...
class MyClass
{
private string _propertyOne;
private string _propertyTwo;
public string PropertyOne { get { return this._propertyOne; } }
public string PropertyTwo { get { return this._propertyTwo; } }
public MyClass(string propertyOne, string propertyTwo)
{
this._propertyOne = propertyOne;
this._propertyTwo = propertyTwo;
}
}
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
myGrid.AutoGenerateColumns = false;
myGrid.Columns.Add("propertyOneColumn", "Property 1");
myGrid.Columns.Add("propertyTwoColumn", "Property 2");
myGrid.Columns["propertyOneColumn"].DataPropertyName = "PropertyOne";
myGrid.Columns["propertyTwoColumn"].DataPropertyName = "PropertyTwo";
myGrid.DataSource = new BindingList<MyClass>(
new MyClass[]
{
new MyClass("big", "small"),
new MyClass("hot", "cold"),
new MyClass("fast", "slow")
}
);
This works very well, and now I have a DataGridView with two columns
and three rows of data being displayed. However, in the real world, we
often deal with composition. For example...
class MyNestedClass
{
private string _propertyThree;
public string PropertyThree { get { return this._propertyThree; } }
public MyNestedClass(string propertyThree)
{
this._propertyThree = propertyThree;
}
}
class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;
public string PropertyOne { get { return this._propertyOne; } }
public string PropertyTwo { get { return this._propertyTwo; } }
public MyNestedClass NestedObject { get { return
this._nestedObject; } }
public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyOne = propertyOne;
this._propertyTwo = propertyTwo;
this._nestedObject = nestedObject;
}
}
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
myGrid.AutoGenerateColumns = false;
myGrid.Columns.Add("propertyOneColumn", "Property 1");
myGrid.Columns.Add("propertyTwoColumn", "Property 2");
myGrid.Columns.Add("propertyThreeColumn", "Property 3");
myGrid.Columns["propertyOneColumn"].DataPropertyName = "PropertyOne";
myGrid.Columns["propertyTwoColumn"].DataPropertyName = "PropertyTwo";
myGrid.Columns["propertyThreeColumn"].DataPropertyName = ?????;
myGrid.DataSource = new BindingList<MyClass>(
new MyClass[]
{
new MyClass("big", "small", new MyNestedClass("medium")),
new MyClass("hot", "cold", new MyNestedClass("warm")),
new MyClass("fast", "slow", new MyNestedClass("steady"))
}
);
As you can see, I don't know what to put for the DataPropertyName for
the third column. Someone previously suggested
"NestedObject.PropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
public string PropertyThree { get { return
this._nestedObject.PropertyThree; } }
To say that this is a half-assed workaround would be modest at best. I
am looking for the proper way to implement this, not a cheap hack. If
anyone knows the right way, I would be very grateful if you would share
your wisdom on this topic.
correctly. It works with a single class, for example...
class MyClass
{
private string _propertyOne;
private string _propertyTwo;
public string PropertyOne { get { return this._propertyOne; } }
public string PropertyTwo { get { return this._propertyTwo; } }
public MyClass(string propertyOne, string propertyTwo)
{
this._propertyOne = propertyOne;
this._propertyTwo = propertyTwo;
}
}
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
myGrid.AutoGenerateColumns = false;
myGrid.Columns.Add("propertyOneColumn", "Property 1");
myGrid.Columns.Add("propertyTwoColumn", "Property 2");
myGrid.Columns["propertyOneColumn"].DataPropertyName = "PropertyOne";
myGrid.Columns["propertyTwoColumn"].DataPropertyName = "PropertyTwo";
myGrid.DataSource = new BindingList<MyClass>(
new MyClass[]
{
new MyClass("big", "small"),
new MyClass("hot", "cold"),
new MyClass("fast", "slow")
}
);
This works very well, and now I have a DataGridView with two columns
and three rows of data being displayed. However, in the real world, we
often deal with composition. For example...
class MyNestedClass
{
private string _propertyThree;
public string PropertyThree { get { return this._propertyThree; } }
public MyNestedClass(string propertyThree)
{
this._propertyThree = propertyThree;
}
}
class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;
public string PropertyOne { get { return this._propertyOne; } }
public string PropertyTwo { get { return this._propertyTwo; } }
public MyNestedClass NestedObject { get { return
this._nestedObject; } }
public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyOne = propertyOne;
this._propertyTwo = propertyTwo;
this._nestedObject = nestedObject;
}
}
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
myGrid.AutoGenerateColumns = false;
myGrid.Columns.Add("propertyOneColumn", "Property 1");
myGrid.Columns.Add("propertyTwoColumn", "Property 2");
myGrid.Columns.Add("propertyThreeColumn", "Property 3");
myGrid.Columns["propertyOneColumn"].DataPropertyName = "PropertyOne";
myGrid.Columns["propertyTwoColumn"].DataPropertyName = "PropertyTwo";
myGrid.Columns["propertyThreeColumn"].DataPropertyName = ?????;
myGrid.DataSource = new BindingList<MyClass>(
new MyClass[]
{
new MyClass("big", "small", new MyNestedClass("medium")),
new MyClass("hot", "cold", new MyNestedClass("warm")),
new MyClass("fast", "slow", new MyNestedClass("steady"))
}
);
As you can see, I don't know what to put for the DataPropertyName for
the third column. Someone previously suggested
"NestedObject.PropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
public string PropertyThree { get { return
this._nestedObject.PropertyThree; } }
To say that this is a half-assed workaround would be modest at best. I
am looking for the proper way to implement this, not a cheap hack. If
anyone knows the right way, I would be very grateful if you would share
your wisdom on this topic.