-->
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.  [ 3 posts ] 
Author Message
 Post subject: DataGridView and NHibernate, updating the database on edit
PostPosted: Wed Oct 17, 2007 12:18 am 
Newbie

Joined: Wed Oct 17, 2007 12:14 am
Posts: 1
Hey

I've been struggeling with this for a while. Is it even possible to use NHibernate and databind it to a DataGridView, and then provide object updates using the ISession ?

I read tons of posts, articles and stuff, but everything is towards READ ONLY. I wish to have EDITABLE data, and be able to add NEW rows, and actually SAVE them in the database afterwards.

I don't see .NET providing me able to do this, as EventListeners such as CellEndEdit only can provide column number, not return the actually object.
I've also looked into http://nhibernate.fluffnstuff.org/ , but it's so poorly documentated, and again only towards READING , or just visual read-only binding in Visual Studio.

Is it totaly impossible to make a DataGridView with editable data, that also will be updated in a database later ? Some users of nhibernate MUST have tried this before, and I would REALLY appreciate help/example on how to do this.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 17, 2007 2:04 pm 
Beginner
Beginner

Joined: Sat Jul 21, 2007 3:56 pm
Posts: 27
Hello windcape,

I have been struggling with the same problems as you describe. But in the end I created my own solution for it.

It is based on Csla's framework, but to get it 100% working (including inline adding and deleting) I had to make my own subclasses of it.

As far as documentation, I will have to disappoint you, because - apart from a lot of comments in the code - there is none available at the moment.

Anyway, if you want to try it, you can find it here: http://www.paarden.be/NHibernate/ObjectFramework.zip Look for the OrganisationsViewer or ClientViewer in the HorsesConfigurator project.

Feel free to ask questions or post your remarks.

TolomaĆ¼s.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 18, 2007 11:49 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
I've done this a few times before. It's a bit of a pain in the rear because I ended up handling the update and insert events manually.

For starters, to populate the GridView I used a DAO as a wrapper like so:
Code:
<asp:ObjectDataSource ID="UserHistoryDataSource" runat="server" SelectMethod="GetAll"
        TypeName="UserHistoryDao">
</asp:ObjectDataSource>

or you can use a dao factory:
Code:
<asp:ObjectDataSource ID="UserHistoryDataSource" runat="server"
                    SelectMethod="GetAll" TypeName="NHibernateDaoFactory+UserHistoryDao">
</asp:ObjectDataSource>


the type UserHistoryDao is used to get all user history records. UserHistoryDataSource is then used with the GridView to populate the grid. Each entity has a property ID that serves as the surrogate key. To retrieve selected items you have to add DataKeyNames="ID" so that the IDs for your entities are available.

To bind properties of classes on the main class bound, you use something like this (on templated column):
Code:
Text='<%# Eval("Name.First") %>'

in the ItemTemplate, This would display the value in UserHistory.Name.First.

I don't have a working example of update and insert on hand, but here's the part that get's most people. You need to make sure that you cancel the update before returning control:
Code:
    Protected Sub WaterMeterModelsView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles WaterMeterModelsView.RowUpdating
        ...
        'Update Code
        'If I remember right, you get the ID from  e.Keys("ID")
        ...

        e.Cancel = True
    End Sub


Inserts are similar, but I hate the inline inserts (I ended up with complex insert templates because of my object graph). I tend to use the insert button on the grid, but when clicked I cancel the insert and redirect the user to an insert page (just my preference).

Quote:
I don't see .NET providing me able to do this, as EventListeners such as CellEndEdit only can provide column number, not return the actually object.

You're right, I'm not aware of a way to get a whole object, but even if you could it would no longer be associated with the session/cache and would force you to make all your entities serializable.

It's a little awkward, but you can make the datagrid (appear) to work like you want id to. If you wanted the DataGrid to do all the work, I think you'd have to have DAOs that work in terms of DataSets (I forget the exact interface supported).

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


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