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.  [ 6 posts ] 
Author Message
 Post subject: Stupid DataGridView question
PostPosted: Mon Sep 18, 2006 12:04 pm 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
Hibernate version: 1.2.0 alpha 1

is it possible to bind a DataGridView datasource to a CreateQuery(...).List() allowing insert and delete from DataGridView ?

With this line i can see and update existing records but i cannot insert/delete anyone; DataGridView is set to allow insert and delete.

Code:
dgvUsers.DataSource = this.m_session.CreateCriteria(typeof(Users)).List();

thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 12:52 am 
Regular
Regular

Joined: Tue Feb 21, 2006 9:50 am
Posts: 107
To enable insert and delete in a DataGridView the list bound to it must implement the interfaces IList and IBindingList. As far as i know there is no collection class in the .NET Framework which implements both interfaces (i haven't much experience with .NET 2.0) so you have to implement your own list class. The code to fill the DataGridView will be like this (assuming that your list implementation provides a static copy() method):

Code:
IList temp = this.m_session.CreateCriteria(typeof(Users)).List();
MyBindingList list = MyBindingList.Copy(temp);
dgvUsers.DataSource = list;


Regards
Klaus


Top
 Profile  
 
 Post subject: Re: Stupid DataGridView question
PostPosted: Tue Sep 19, 2006 1:21 am 
Beginner
Beginner

Joined: Thu Jun 29, 2006 12:32 pm
Posts: 22
berets wrote:
Hibernate version: 1.2.0 alpha 1

is it possible to bind a DataGridView datasource to a CreateQuery(...).List() allowing insert and delete from DataGridView ?

With this line i can see and update existing records but i cannot insert/delete anyone; DataGridView is set to allow insert and delete.

Code:
dgvUsers.DataSource = this.m_session.CreateCriteria(typeof(Users)).List();

thanks a lot


You should not bind to the IList directly, but wrap it in a BindingList.

1. Derive a class from BindingList.

2. Take the ISet / IList as a parameter to the constructor, and copy all the items in the ISet / IList to your derived binding list (it's just copying references, so no big deal).

3. Override the Insert and Remove methods, and add / remove from the BindingList as well as the internal ISet / IList.

4. Make sure you call ISession.Flush to post to the database. This would be best handled with a 'Save' option.

Cheers,

Stu

_________________
--
Stuart Carnie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 4:24 am 
Regular
Regular

Joined: Thu May 11, 2006 12:30 pm
Posts: 72
thanks to all, i did it

Code:
public class BindableList<T> : BindingList<T> where T : Users


and now DataGridView allow me to insert and delete, but when i call the Save method of my session which parameter i need to pass ?

i try

Code:
this.m_session.Save(dgvUtenti.DataSource);


or

Code:
// this version of my class manage the source IList objetc as a property
// and return it for save
this.m_session.Save((dgvUsers.DataSource as BindableList<Users>).List);


but i always getting an exception


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 19, 2006 4:36 am 
Beginner
Beginner

Joined: Thu Jun 29, 2006 12:32 pm
Posts: 22
berets wrote:
thanks to all, i did it

Code:
public class BindableList<T> : BindingList<T> where T : Users


and now DataGridView allow me to insert and delete, but when i call the Save method of my session which parameter i need to pass ?

i try

Code:
this.m_session.Save(dgvUtenti.DataSource);


or

Code:
// this version of my class manage the source IList objetc as a property
// and return it for save
this.m_session.Save((dgvUsers.DataSource as BindableList<Users>).List);


but i always getting an exception


You'd be best to track the new entities added to your list, and call ISession.SaveOrUpdate on each one before ISession.Flush.

_________________
--
Stuart Carnie


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 13, 2007 9:25 am 
Newbie

Joined: Tue Mar 06, 2007 4:46 am
Posts: 7
I'm currently trying to bind a datagrid to a generic ISet. I've found many advices how to go ahead with this topic but I cannot believe that there's nothing included in NHibernate to bind UI controls to NHibernate-collections.

I tried to follow the solution above but in that case I've to implement a class for every object. When I implement a generic class I get the error, that ISet<T> cannot be converted to T[] set.CopyTo(this, 0) is executed.

Has anybody a code-sample how to bind a datagrid to an ISet? Any changes made to the data (update, insert, delete) should be handled by the binding...


Here is my coding how I tried to copy an ISet to a BindingList...

Code:
    public class BindableList<T> : BindingList<T>
    {
        ISet<T> _set;

        public BindableList(ISet<T> set)
        {
            set.CopyTo(this, 0);
            this._set = set;
        }

        protected override void RemoveItem(int index)
        {
            base.RemoveItem(index);

            this._set.Remove(base[index]);
        }

        protected override void InsertItem(int index, T item)
        {
            base.InsertItem(index, item);

            this._set.Add(item);
        }
    }


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