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).