-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to bind nested class objects to DataGridView in C#?
PostPosted: Tue Jun 17, 2008 3:46 am 
Newbie

Joined: Tue Sep 06, 2005 4:12 am
Posts: 16
Hibernate version: 1.2.0

Hi all,

I have 2 classes like this:

Code:
namespace NHibernate.Examples.UserDeptModule
{
    public class Dept
    {
        private Guid id;
        private String deptName;

        public virtual Guid Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual String DeptName
        {
            get { return deptName; }
            set { deptName = value; }
        }       

        public override String ToString()
        {
            return deptName;
        }
    }
}

namespace NHibernate.Examples.UserDeptModule
{
    public class User
    {
        private Guid id;
        private Guid dept_id;
        private Dept dept;
        private string userName;

        public User()
        {
        }

        public virtual Guid Id
        {
            get { return id; }
            set { id = value; }
        }

        public virtual Guid Dept_Id
        {
            get { return dept_id; }
            set { dept_id = value; }
        }

        public virtual Dept DeptRef
        {
            get { return dept; }
            set
            {
                if (value != null)
                {
                    this.dept_id = value.Id;
                }
                dept = value;
            }
        }

        public virtual string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

    }
}


My project is under NHibernate, and two class is done in many-to-one manner. Now I want to show user information to DataGridView including Dept's deptName and deptTel. How to do this?

Thank you very much.

--
Best regards,
Diviner.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 10:58 pm 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
I use a DTO to flatten the hierarchy: http://elegantcode.com/2007/11/06/dtos-presentation-model-and-icriteria/

You should be able to avoid DTOs by using the WinForms equivalent of an ASP.NET 2.0 DataGrid's RowDataBound event (sorry it's been a long time since I did any WinForms development). However I think that the DTO approach is superior for many reasons. For example it protects the UI from changes in the domain and makes it very clear what data is used by which screen.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 2:58 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:09 pm
Posts: 23
So far as I know there's no *easy* way to do this, but you can implement System.ComponentModel.ITypedList to create a view of the data.

Have a look at the following articles for more info:
http://weblogs.asp.net/fbouma/articles/115837.aspx
http://www.codeproject.com/KB/cs/comple ... nding.aspx

If you are feeling masochistic you could create a fairly generic version so that for a given type you can specify named property chains. Something like:

Code:
cm = new ClassModel(User)
cm.AddProperty("DeptName","DeptRef.DeptName")
cm.AddProperty("DeptId","DeptRef.Id")

ta = new TypedArray(cm, userList)
control.DataSource = ta


There might be easier ways in 2.0+, but I'm still using .net 1.1.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 28, 2008 7:59 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
Have a look at http://www.codeproject.com/KB/cs/Object ... ource.aspx, it does exactly what Tristian described.

I've used it once and it worked fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 3:28 am 
Newbie

Joined: Thu Nov 29, 2007 4:11 am
Posts: 2
Thanx for the links - articles are alright ;)

_________________
http://www.love-vs-hate.com/ - tupac
http://www.mybestcity.com/ - amaru
http://www.newbabyassistant.com/ - shakur


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.