-->
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.  [ 4 posts ] 
Author Message
 Post subject: Late Databinding to a DataGridView using an IList
PostPosted: Thu May 11, 2006 9:59 pm 
Newbie

Joined: Mon May 08, 2006 11:03 am
Posts: 12
Here's my query:
Code:
String sql = "SELECT stud.FirstName, stud.LastName, stud.Id, stud.ATANumber, max(b.RankValue), bd.Description" +
                   
                    " FROM TKD.Student as stud, Belt as b, BeltDesc bd WHERE b.StudentID = stud.Id AND bd.RankVal = b.RankValue " +
                    " GROUP BY stud.FirstName, stud.LastName, stud.Id, stud.ATANumber, b.RankValue, bd.Description ";

To which I use here:
Code:
studentList = session.CreateQuery(sql).List() ;

And I get a list of values I'm expecting. However, when I try to do some late databinding, I have no idea how to bind the columns.

Code:
this.dgStudentList.Columns[0].DataPropertyName = "Student.FirstName";
                this.dgStudentList.Columns[1].DataPropertyName = "TKD.BeltDesc.Description";
                this.dgStudentList.Columns[2].DataPropertyName = "stud.ATANumber";


Those three lines show the various things I've tried. Now as I've debugged, I built an IList of just student records, and saw each element had a {TKD.Student} type. but with the studentList above, each line was listed with {Dimension:[6]} type.

Is there a way I can do the databinding like I'm expeciting above, or is there a different way to do it?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 4:16 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
thav,

is there a particular reason why you are writing your own sql and not using HQL or createCriteria? it really is a lot simpler:

Code:
<class name="student">
    <property name="firstName" />
    ...
    <bag name="belts">
        <one-to-many class="belt" />
    </bag>
</class>

<class name="belt">
    <property name="rankValue" />
    ...
    <one-to-one name="description" class="beltDescription" />
</class>


then, in your DAO (or however you are designing your Data Access Layer):

Code:
public IList getAllStudents(params string[] sortFields) {
    ICriteria crit = session.CreateCriteria(typeof(Student));

    if (sortFields != null) {
        foreach (string sortField in sortFields) {
            c.AddOrder(Order.Asc(sortField));
        }
    }

    return c.List();
}


then somewhere in your presentation layer (codebehind file):

Code:
DataGrid myStudentDG;

myStudentDG.DataSource = myDAO.GetAllStudents();
myStudentDG.DataBind();


and in your aspx file you can bind specific fields using the

Code:
<%# DataBinder.Eval(Container.DataItem, "FieldName") %>


notation. So something like:

Code:
<asp:datagrid id="AnswerSet" AutoGenerateColumns="False" Runat="server">
   <Columns>
      <asp:TemplateColumn>
         <HeaderTemplate>FirstName</HeaderTemplate>
         <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "FirstName") %></ItemTemplate>
         <EditItemTemplate><asp:TextBox ID="FirstName" CssClass="textfield" Enabled=False Text='<%# DataBinder.Eval(Container.DataItem, "FirstName") %>' Runat=server /></EditItemTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn>
         <HeaderTemplate>Belt Rank</strong></HeaderTemplate>
         <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "Belt.RankValue") %>
         </ItemTemplate>   
      </asp:TemplateColumn>
   <asp:EditCommandColumn ButtonType=LinkButton EditText="Edit" CancelText="Cancel" UpdateText="Update" />
   </Columns>
</asp:datagrid>


obviously there is much more you can do with the datagrid, but by having your DAO return a list of student objects, you can simply walk the object graph to populate the DG. much easier that trying to get NH to return "resultsets" or whatever it is that Microsoft expects.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 8:42 pm 
Newbie

Joined: Mon May 08, 2006 11:03 am
Posts: 12
Thanks for the reply,

I guess I thought I was using HQL with that CreateQuery call. I'm going to try your mappings to see if that gives me anything.

I'm also using a windows forms application, binding to a DataGridView.

-----
I mapped the bag correctly, but I now get a runtime error:

Unable to cast object of type 'NHibernate.Collection.Bag' to type 'NHibernate.Collection.List'

I was getting this before, which is why I used the list.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 10:26 pm 
Newbie

Joined: Mon May 08, 2006 11:03 am
Posts: 12
Never mind, that's an ID 10 T error. I forgot about my class definition.


New issue now - the belt -> beltdesc one-to-one relationship isn't working. Instead of finding on rankvalue for both tables, it finds on the ID.

so if belt.id = 15, it pulls in beltdesc.id = 15.

(as an aside, beltdesc is a read-only table).

I'm going to check my logs, but I wanted to get this posted, so someone doesn't try to correct my idiocy.


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