473,580 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView / DataPropertyNam e - 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._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }

public MyClass(string propertyOne, string propertyTwo)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
}
}
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";

myGrid.DataSour ce = new BindingList<MyC lass>(
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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}
}

class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;

public string PropertyOne { get { return this._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }
public MyNestedClass NestedObject { get { return
this._nestedObj ect; } }

public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
this._nestedObj ect = nestedObject;
}
}
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");
myGrid.Columns. Add("propertyTh reeColumn", "Property 3");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = ?????;

myGrid.DataSour ce = new BindingList<MyC lass>(
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 DataPropertyNam e for
the third column. Someone previously suggested
"NestedObject.P ropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
public string PropertyThree { get { return
this._nestedObj ect.PropertyThr ee; } }
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 22833
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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}

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

Now you must change the DataPropertyNam e as bellow,
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = "NestedObje ct";

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._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }

public MyClass(string propertyOne, string propertyTwo)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
}
}
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";

myGrid.DataSour ce = new BindingList<MyC lass>(
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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}
}

class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;

public string PropertyOne { get { return this._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }
public MyNestedClass NestedObject { get { return
this._nestedObj ect; } }

public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
this._nestedObj ect = nestedObject;
}
}
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");
myGrid.Columns. Add("propertyTh reeColumn", "Property 3");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = ?????;

myGrid.DataSour ce = new BindingList<MyC lass>(
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 DataPropertyNam e for
the third column. Someone previously suggested
"NestedObject.P ropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
public string PropertyThree { get { return
this._nestedObj ect.PropertyThr ee; } }
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._propertyT hree; } }
public string PropertyFour { get { return this._propertyF our; } }

public MyNestedClass(s tring propertyThree, string propertyFour)
{
this._propertyT hree = propertyThree;
this._propertyF our = 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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}

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

Now you must change the DataPropertyNam e as bellow,
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = "NestedObje ct";

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._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }

public MyClass(string propertyOne, string propertyTwo)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
}
}
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";

myGrid.DataSour ce = new BindingList<MyC lass>(
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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}
}

class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;

public string PropertyOne { get { return this._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }
public MyNestedClass NestedObject { get { return
this._nestedObj ect; } }

public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
this._nestedObj ect = nestedObject;
}
}
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
myGrid.AutoGene rateColumns = false;

myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");
myGrid.Columns. Add("propertyTh reeColumn", "Property 3");

myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = ?????;

myGrid.DataSour ce = new BindingList<MyC lass>(
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 DataPropertyNam e for
the third column. Someone previously suggested
"NestedObject.P ropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
public string PropertyThree { get { return
this._nestedObj ect.PropertyThr ee; } }
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._propertyT hree; } }
public string PropertyFour { get { return this._propertyF our; } }

public MyNestedClass(s tring propertyThree, string propertyFour)
{
this._propertyT hree = propertyThree;
this._propertyF our = 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._propertyT hree; } }

public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}

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

Now you must change the DataPropertyNam e as bellow,
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = "NestedObje ct";

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._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }
>
public MyClass(string propertyOne, string propertyTwo)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
}
}
>
>
I can create a DataGridView and bind it to a BindingList of MyClasses
as follows:
>
>
myGrid.AutoGene rateColumns = false;
>
myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");
>
myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";
>
myGrid.DataSour ce = new BindingList<MyC lass>(
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._propertyT hree; } }
>
public MyNestedClass(s tring propertyThree)
{
this._propertyT hree = propertyThree;
}
}
>
class MyClass
{
private string _propertyOne;
private string _propertyTwo;
private MyNestedClass _nestedObject;
>
public string PropertyOne { get { return this._propertyO ne; } }
public string PropertyTwo { get { return this._propertyT wo; } }
public MyNestedClass NestedObject { get { return
this._nestedObj ect; } }
>
public MyClass(string propertyOne, string propertyTwo,
MyNestedClass nestedObject)
{
this._propertyO ne = propertyOne;
this._propertyT wo = propertyTwo;
this._nestedObj ect = nestedObject;
}
}
>
>
Unfortunately, this is where I start having a problem because now I
have to adjust the code for the DataGridView...
>
>
myGrid.AutoGene rateColumns = false;
>
myGrid.Columns. Add("propertyOn eColumn", "Property 1");
myGrid.Columns. Add("propertyTw oColumn", "Property 2");
myGrid.Columns. Add("propertyTh reeColumn", "Property 3");
>
myGrid.Columns["propertyOneCol umn"].DataPropertyNa me = "PropertyOn e";
myGrid.Columns["propertyTwoCol umn"].DataPropertyNa me = "PropertyTw o";
myGrid.Columns["propertyThreeC olumn"].DataPropertyNa me = ?????;
>
myGrid.DataSour ce = new BindingList<MyC lass>(
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 DataPropertyNam e for
the third column. Someone previously suggested
"NestedObject.P ropertyThree," however this does NOT work. Yet another
suggestion was to create a public accessor in MyClass like this:
>
>
public string PropertyThree { get { return
this._nestedObj ect.PropertyThr ee; } }
>
>
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
1727
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
2012
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, Flash, JPEG and ASCII art versions. The first two are there because they print at much higher resolution than the JPEG, so I'm keen to keep them in...
3
2684
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 nested class, I get the error message about "this" (the parent being readonly). When I leave out the ref keyword things compile, but I want to make...
1
7494
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 properites exposes another object I would like to bind a property of the sub object to the DataPropertyName this does not seem to work. eg main...
0
1525
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 expand the property) and allow those values to be edited. My code is below. The grid is showing the values but they are read-only, although the other...
4
3200
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. However there is a problem with displaying properties of object B. Is there some way to bind labels to ObjectA.ObjectB.Property? Or do I have to...
3
2287
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, but the data only appears if I select a cell otherwise the DGV look empty apart from the selected cell.
4
9621
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 when I attempt to enter the new row. My guess is that I need to override some method so that I can create the object for the grid. But for the life...
10
2458
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 $_currentValue; private $_origin;
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6533
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5349
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3790
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3806
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1394
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.