Windows Develop Bookmark and Share   
 index > Windows Forms General > bind nested objects to datagridview in vs.net 2005
 

bind nested objects to datagridview in vs.net 2005

Is there easy way to bind nested object to datagridview? Also, I have 3 different data sources, I would needswitch amongthem when needed. I havethree DataGridViewCustomColumn functions set up for those different source, when Iswitchdata source, datagridview is not clearing previous rows, dgv is appending with current datasource iinfo. How to clear previous sources? I tried with datagridview.databinds.clear(),didn't work as epxected. I do not want to bind my list directly, b'case it is not more than I need to display to the user.

the below link explains about nested objects binding to datagridview, just wondering if there is different way. Also, I'm using NHibernate, it is little different than regular dal.
http://www.codeproject.com/KB/cs/ObjectBindingSource.aspx

Thanks.
GSReddy  Monday, September 28, 2009 3:25 AM
To answer your first question, no. There is no easy way to bind nested objects to the DataGridView. It expects everything at the same level.

For example, Person class with LastName, FirstName, MiddleName.
Address class with Street, City, State, Zip.

If you want all of these fields to appear in your DataGridView using object binding, you have to expose read-only properties of the Address class directly in the Person class.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Tuesday, September 29, 2009 3:35 PM

Thank for reply.

I did type these sample for my understanding. This is similar class structure I have.

dataGridView1.DataSource = objPerson; // assume person object have data.

Class Person
{
 string _lName;
 Address _address = null;

 Public string LName
 {
  get{return _lName;}
  set{_lName = value;}
 }
  
  Public Address PAddress
  {
  get{return _address;}
  set{_address = value;}
  }
}

Class Address
{
string _street;

 Public string Street
 {
  get{return _street;}
  set{_street = value;}
 }
}

This will not work, as you said I do not have readonly properties set up for address class in Person class.
Class Person
{
......
.......

 Public string Street
 {
   get {new Address().Street;}
 }


Is this the way I have to modify Person class to display street in gridview? the problem doing so is, I have NHibernate set up as the DAL layer. So, Hibernate needs mapping xml file for every class. I have one xml for person and another one for Address. As you sugested, by adding readonly property to Peson class needs to modify mapping xml. So, again I need copy same mapped types from Addres xml to person xml. This is easy to add, but I'm thinking of redundancy. 

Thanks. 

GSReddy  Saturday, October 03, 2009 8:57 PM
That is the basic idea, but I think you would want to use PAddress.Street and not a new Address.

Yes, it is going to be redundant ... but if you want to data bind to a DataGridView this is the only way I know how to achieve it.

Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Monday, October 05, 2009 3:28 PM

You can use google to search for other answers

Custom Search

More Threads

• Treeview Problem
• Hide a prgram from task manager
• Compilation Error
• How to set focus on message box in Console Applications
• script error in axwebbrowser and newwindow pdisp problem in normal webbrowser(not axwebbrowser)
• IndexFromPoint and Owner Draw Variable Issues
• bending edges
• Open the Research Pane in Office 2003 using C#
• displaying large image in Windows Form
• Localization resources