473,385 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DataGridView / DataPropertyName - Displaying nested object information

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.

Nov 16 '06 #1
3 22772
Hi,

If you try to display an object in DataGridView, it will display the string
property of the object. So, 1st you must override the ToString() method of
MyNestedClass.

class MyNestedClass
{
private string _propertyThree;

public string PropertyThree { get { return this._propertyThree; } }

public MyNestedClass(string propertyThree)
{
this._propertyThree = propertyThree;
}

public override string ToString()
{
return this._propertyThree;
}
}

Now you must change the DataPropertyName as bellow,
myGrid.Columns["propertyThreeColumn"].DataPropertyName = "NestedObject";

Cheers,
Chester

"connected" wrote:
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.

Nov 16 '06 #2
Thanks for the prompt response. Unfortunately, while valid in this
particular case, this is not a universal solution. Consider the
possibility that MyNestedClass may have more properties that must also
be displayed in their own columns on the DataGridView, as in...
class MyNestedClass
{
private string _propertyThree;
private string _propertyFour;

public string PropertyThree { get { return this._propertyThree; } }
public string PropertyFour { get { return this._propertyFour; } }

public MyNestedClass(string propertyThree, string propertyFour)
{
this._propertyThree = propertyThree;
this._propertyFour = propertyFour;
}
}
Now the ToString() hack will not work because it can only return one
property at a time.

Chester wrote:
Hi,

If you try to display an object in DataGridView, it will display the string
property of the object. So, 1st you must override the ToString() method of
MyNestedClass.

class MyNestedClass
{
private string _propertyThree;

public string PropertyThree { get { return this._propertyThree; } }

public MyNestedClass(string propertyThree)
{
this._propertyThree = propertyThree;
}

public override string ToString()
{
return this._propertyThree;
}
}

Now you must change the DataPropertyName as bellow,
myGrid.Columns["propertyThreeColumn"].DataPropertyName = "NestedObject";

Cheers,
Chester

"connected" wrote:
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.
Nov 16 '06 #3
Hi,

Good point, but DataGridView doesn't support databinding to child
properties. For more info, check this post -
http://forums.microsoft.com/MSDN/Sho...34204&SiteID=1

Cheers,
Chester

"connected" wrote:
Thanks for the prompt response. Unfortunately, while valid in this
particular case, this is not a universal solution. Consider the
possibility that MyNestedClass may have more properties that must also
be displayed in their own columns on the DataGridView, as in...
class MyNestedClass
{
private string _propertyThree;
private string _propertyFour;

public string PropertyThree { get { return this._propertyThree; } }
public string PropertyFour { get { return this._propertyFour; } }

public MyNestedClass(string propertyThree, string propertyFour)
{
this._propertyThree = propertyThree;
this._propertyFour = propertyFour;
}
}
Now the ToString() hack will not work because it can only return one
property at a time.

Chester wrote:
Hi,

If you try to display an object in DataGridView, it will display the string
property of the object. So, 1st you must override the ToString() method of
MyNestedClass.

class MyNestedClass
{
private string _propertyThree;

public string PropertyThree { get { return this._propertyThree; } }

public MyNestedClass(string propertyThree)
{
this._propertyThree = propertyThree;
}

public override string ToString()
{
return this._propertyThree;
}
}

Now you must change the DataPropertyName as bellow,
myGrid.Columns["propertyThreeColumn"].DataPropertyName = "NestedObject";

Cheers,
Chester

"connected" wrote:
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.
>
>

Nov 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: M P | last post by:
I have an MS Access DB and wanting to display OLE Object(pictures) data type of MS Access to the browser. Is there a way that I can call it from the database? Mark
10
by: Philip Ronan | last post by:
Hi, I'm having problems embedding alternative content with nested OBJECT tags. Take a look at <http://www.japanesetranslator.co.uk/chihiro/map.html> The map on this page is available in SVG,...
3
by: mbaskey | last post by:
Hello... I have an object which has a nested object which need to look at the parent to return a value about it. Basically as below but when I put the ref method parameter to construct the...
1
by: WayDownUnder | last post by:
I have a datagridview that is bound to a cutom collection. The classes contained in the custom collection have the properties that are bound to the class . This works fine ! But if one of the...
0
by: movieknight | last post by:
Hi, I have a class which I am feeding to the propertygrid, and I am exposing a Mesh object from my class for the propertygrid to display. I want the propertygrid to show the values (when you...
4
by: Piotrek | last post by:
Hi all. I have a web app, in which I have object A. Object A has some properties plus it holds instance of object B. I bound object A to FormView - all its properties are properly displayed....
3
by: RichT | last post by:
Hi all, I am experiencing some odd behaviour with a DataGridView. The DataGridView is bound to a DataTable, which is populated with data from a csv file. The column Headings appear fine,...
4
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a DataGridView bound to a collection of Custom Objects. I have enabled adding new rows, but since my object does not have a public constructor (it uses a factory method), I get an error...
10
guillermobytes
by: guillermobytes | last post by:
Hi, I'm creating a class, that clones itself whenever it gets a new value. Each instance has a set of properties that are related to the value it gets: class Atom { private...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.