-->
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.  [ 12 posts ] 
Author Message
 Post subject: Datagrid binding with Nhibernate objects
PostPosted: Wed Aug 31, 2005 11:25 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
I realize that this may be slightly off-topic.

Let us say I have Customer -> Orders classes. I am databinding the Orders collection to ASP.Net datagrid. In the datagrid, I am showing things like Product#, quantity etc. but I also want to show the Customer Name, for example. Or I may want to display other non-scalar properties of the Order object, like order.product.name.

How can I do this? It seems that when I databind the orders collection to the datagrid, only scalar values are databound.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 5:26 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
An ObjectView might be the solution :wink:

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 02, 2005 7:45 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
KPixel wrote:
An ObjectView might be the solution :wink:


Well, in fact no, because the ASP.NET databinding is completely different. It doesn't use IBindingList/ITypedList etc..

In an ASP.NET datagrid you can turn off autogenerating columns and specify the properties that are shown in the grid. This also can be properties like 'order.Product.Name'.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 15, 2005 3:26 am 
Really Cuyahoga?

Are you using asp.net 1.1 or 2.0?

I have been unable to databind a field to a property of a sub-object in asp.net 2.0....


Top
  
 
 Post subject:
PostPosted: Sat Oct 15, 2005 4:30 am 
Senior
Senior

Joined: Sat May 14, 2005 8:40 am
Posts: 130
Well, I haven't tried it yet in ASP.NET 2.0, but I can't imagine why it wouldn work anymore.
Try to create a template column in a grid and in the template do something like <%# DataBinder.Eval(Container.DataItem, "Customer.Name") %>

I think they even simplified this in ASP.NET 2.0 so you can do <%# Eval("Customer.Name") %>.

_________________
Cuyahoga


Top
 Profile  
 
 Post subject: I use ado.net as an interface
PostPosted: Fri Oct 21, 2005 12:03 pm 
I had the same problem when I started using NHibernate as data accessing engine. The problem comes from the difference of strutures between datagrid and object tree.

When I am doing now is create a ADO.NET DataTable on the fly:

Code:
IList list = orderService.GetOrders();

DataTable table = new DateTable();
table.Columns.Add( "Name" );
table.Columns.Add( "Description" );
table.Columns.Add( "Customer" );

foreach( Order o in pList )
{   
  object[] row= { o.Product.Name,
         o.Description + "Whatever. Here is the flexibility",
         o.Customer.Name
         };
  table.Rows.Add( row );
}


This way, I have the full control on what I will dispay on in the grid.

Hope this helps.

Hongze


Top
  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 4:55 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 4:59 am
Posts: 26
Location: Cambridge, UK
I haven't tried .NET 2.0 yet, but the code martijnb describes works perfectly for me in 1.1. I bind datagrids to NHibernate-produced collections all the time.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 8:50 pm 
Beginner
Beginner

Joined: Mon Aug 15, 2005 11:50 pm
Posts: 22
martijnb wrote:
Try to create a template column in a grid and in the template do something like <%# DataBinder.Eval(Container.DataItem, "Customer.Name") %>


One problem I've been unable to solve when showing data this way in a datagrid when using a custom Telerik Grid control and specifying a column as the sort coulmn. I think the custom grid creates its own dataview and is unable to comprehend 'Parent.Children' objects. It works fine with just 'Parent' fields for columns.

The only way I've found to work round this is to build my own datatable first to flatten the information, like hzwang demostrated.

Perhaps a better approach is to override the databind in the control, a bit beyond my level...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 04, 2005 8:53 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
theres no reason to create a datatable to flatten your results. Databinding in asp.net is simple, you just have to cast the object to correct type like this:

Code:
<itemtemplate>
   <%#((Member)Container.DataItem).Role.Name%>
</itemtemplate>


This code is taken directly from one of my asp.net pages. Using the Databinder.eval operation is slower because it uses reflection.

You just have to make sure that you add an import statement to include the namespace in your aspx page and turn off autogeneratecolumns as martijnb suggested.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 9:53 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 4:59 am
Posts: 26
Location: Cambridge, UK
Ah, that's much cleaner looking. Thanks, jnapier; I'll definitely have to remember that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 4:09 am 
Ok it's work perfect but olny like Eval - but this is readolny mode. If you want make updat-save operation then what? Bind("member.Role.Name") occurs error during compile. How to solve this problem in 2.0 ?


Top
  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 5:31 pm 
Newbie

Joined: Wed Nov 02, 2005 9:52 am
Posts: 18
Location: Argentina
I have the same problem, but in version 1.1 the data is read only, when try to sort the data by "Order.Customer.Name" can't find the "Customer.Name" object inside the Order list

Any workaround for this?

Tks !


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.