-->
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.  [ 9 posts ] 
Author Message
 Post subject: DataGrid showing properties instead of data
PostPosted: Sat Apr 21, 2007 2:24 am 
Newbie

Joined: Sat Apr 21, 2007 2:12 am
Posts: 16
Hi All

The below stated query returns Length of Product rather than data.I tried joins as well but same results.Please help.


Code:
   

string query = "Select p.Product From PurchaseOrderLine p where p.POId = '" + txtPOID.Text + "'";

ISession session = null;
           
            try
            {
                session = Global.NHSession;
                this.grid1.DataSource = session.Find(query);                                 
            }
            finally
            {
                session.Close();
            }


       




Thanks

Regards,
Gaurav


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 2:58 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

What is 'Product' : a data field or an object ?

If Product is an object, you'll only see the ToString() result of it in the DataGridView. You might try binding the grid to a DataBindingSource which is in turn bound to an object of the Product-type. That way you can create the appropriate columns in your DataGridView and bind you Product object to the DataBindingSource.

X.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 6:50 am 
Newbie

Joined: Sat Apr 21, 2007 2:12 am
Posts: 16
Thanks for the reply.

I am sorry, I could not get you. Could you please give me an example.

Thanks

Regards,
Gaurav


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 7:30 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

First, drop a DataBindingSource on your form.
In the properties of the DataBindingSource, locate the DataSource property and point it to your Product-class (you'll have to create an Object-datasource with the "Add data source ..." option).

In the properties of your grid, point the DataSource property to your newly created DataBindingSource. The columns should be generated automatically.

When fetching you object from NHibernate, do not assign it to the DataSource property of the DataGridView but assign it to the DataSource property of the DataBindingSource instead.
You should see the data in the datagridview now.

(The DataBindingSource is in fact superfluous, it's just easier this way).

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 8:09 am 
Newbie

Joined: Sat Apr 21, 2007 2:12 am
Posts: 16
Thanks xasp..I learned a new way of fetching data from you :)

But it does not solve my problem. data was being fetched earlier also.My problem is that i want to fetch data from selected columns of a single table through Object file using NHibernate.

I have a table name PoLine against which i have created PurchaseOrderLine class having all the getters and setters.

*****************************

string query = "Select p.Product From PurchaseOrderLine p where p.POId = '" + txtPOID.Text + "'";

ISession session = null;

try
{
session = Global.NHSession;
this.grid1.DataSource = session.Find(query);
}
finally
{
session.Close();
}
*********************************


The above stated code is no doubt fetching the product names from PurchaseOrderLine , but also fetching other columns with no value.
I want that grid should not show other columns when I am writing query to fecth products only.

---------------------------------------------------------------
Product POId Price Quantity TotalLinePrice |
--------------------------------------------------------------|
Product A 2 0 0 0 |
Product B 2 0 0 0 |
---------------------------------------------------------------

This is what it has fetched in datagrid. I want only those columns should be visible dynamically which are being fetched in my select query. Like if i write "Select p.Product....." then one column should be created dynamically in grid and if i write "Select p.Product,p.Price...." thwo columns should be created dynamically in the grid.

Hope I am clear what i wanted to ask :)

Please help

Regards,
Gaurav

PS: From where to give u ranking?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 3:28 pm 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
Is Product a mapped class? If so, this query,
Quote:
"Select p.Product From PurchaseOrderLine p ..."

will return a list of Product objects (or their proxy). I'm assuming that your datagrid is using automatically generated columns only (if not, please mention what the columns are and what they're bound to), so binding to this list of Products will expose all public properties of Product.

If Product isn't a mapped class, could you post the relevant sections of the PurchaseOrderLine mapping?

Quote:
If i write "Select p.Product....." then one column should be created dynamically in grid and if i write "Select p.Product,p.Price...." two columns should be created dynamically in the grid.

Trying to do that won't work without more effort on your part. Even if NHibernate were to return a sparsely-populated PurchaseOrderLine p object (which it won't with this query), binding a datagrid to that object would still create a column for each public property. If there are multiple items in a select clause, NHibernate will return an object[] for each row. With "Select p.Product, p.Price", each row returned will be an object[] of size two with ary[0]=p.Product and ary[1]=p.Price. Trying to bind a datagrid to an array of objects will only expose properties of the Array class, not the actual data in the array.

One approach would be to have a query "select p from ..." and programmatically hide the columns of the datagrid you aren't using, but that could result in returning a lot more data from the db than necessary. I also don't know if this would work with automatically-generated columns; you may need to specify each column and what it binds to before you can hide them. Another approach would be to use the queries as they're written and use reflection to dynamically create a class that only had the required properties. Then you could loop over the returned rows and assign each object in the array to the correct property of a new instance of the dynamically generated class and bind the datagrid to that list. That would be more difficult to set up, but would only fetch the necessary data from the db. You could also probably do this by implementing the ITypedList interface, but then again you'd need to either get all the data from the DB and hide some of it or iterate over the object arrays returned from the query and putting the data into new instances of PurchaseOrderLine.


Last edited by JWLato on Tue Apr 24, 2007 10:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 2:28 am 
Newbie

Joined: Sat Apr 21, 2007 2:12 am
Posts: 16
Hi JWLato

Thanks for the reply. No, Product is not a class but a property od PurchaseOrderLine class which has been mapped to POLine table in SQLServer. Here is the mapping class

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" default-access="property">

<class name="Wilson.NHibernate.Example.PurchaseOrderLine, WilsonNHibernateExample" table="POLine">
<id name="LineId" column="POLineId">
<generator class="assigned" />
</id>
<property name="POId" column="POId" />
<property name="Product" column="Product" />
<property name="Quantity" column="Qty" />
<property name="Price" column="Price" />
<property name="TotalLinePrice" column="TotalLinePrice" />
<bag name="Lines" table="POLine" cascade="none" access="nosetter.camelcase" lazy="false" inverse="false">
<key column="POId" />
<one-to-many class="Wilson.NHibernate.Example.PurchaseOrderLine, WilsonNHibernateExample" />
</bag>

</class>


</hibernate-mapping>


Well, now I am able to hide non-required column pro grammatically. But a new problem is existing now. After grid gets loaded, I am making changes in data in cells. And when i call Session.Update(grid1)...It does not save changed values. It saves same old values that had been binded to it.Why is it so?

Thanks

Regards,
Gaurav


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 24, 2007 10:39 am 
Newbie

Joined: Mon Apr 02, 2007 12:31 pm
Posts: 19
Hi Gaurav,

Quote:
And when i call Session.Update(grid1)...It does not save changed values.

Is grid1 the DataGrid instance? Session.Update() only works with NHibernate-persisted objects, i.e. objects for which you've written a mapping. If you specify an object that NHibernate isn't aware of, Session.Update() won't do anything. In your event handler for updating the data, you'll need to load the correct PurchaseOrderLine object, change the data to the new value(s), then call Session.Update() on the PurchaseOrderLine object.

If you're using the data editing feature built-in to the datagrid, I probably won't be able to offer much further advice. I usually create my own web controls for editing data instead of using the datagrid features.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 12:15 am 
Newbie

Joined: Sat Apr 21, 2007 2:12 am
Posts: 16
Thanks JWLato

Your reply has been a great help for me :)

Regards,
Gaurav


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