Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / October 2005

Tip: Looking for answers? Try searching our database.

datagrid and custom collection object synchronization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sanjin - 12 Oct 2005 07:34 GMT
I have already posted this on microsoft.public.dotnet.general, but without
answer.

I have custom collection that is bind to the System.Windows.Forms.DataGrid.

My custom collection business object has property Quantity.

public event System.ComponentModel.PropertyChangedEventHandler
QuantityChanged;
private int quantity;
[System.ComponentModel.Bindable(true)]
public int Quantity
{
get { return quantity; }
set
{
if (quantity != value)
{
            quantity = value;
            if (QuantityChanged != null)
 QuantityChanged(this, new
System.ComponentModel.PropertyChangedEventArgs("Quantity") );
}
}
}

Property Quantity is modified through code and datagrid quantity is not
automatically refreshed (QuantityChanged is always null)

Is it possible to somehow accomplish datagrid and collection synchronization
automatically?

I know that I can do something like that, but I want nicer solution (pseudo
code):

MyCollection.QuantityChanged += new
PropertyChangedEventHandler(refresh_method);

...

private void refresh_method()
{
   myDataGrid.Refresh();
}

Thanks in advance

Sanjin
Bart Mermuys - 12 Oct 2005 12:46 GMT
Hi,

>I have already posted this on microsoft.public.dotnet.general, but without
>answer.
[quoted text clipped - 29 lines]
> synchronization
> automatically?

Your Custom Collection needs to implement IBindingList and return true for
SupportsChangeNofication.  Then each object must inform the Custom
Collection if one if its property changed.   Then the Custom Collection
fires the ListChanged event (available if you implement IBindingList).

The objects can also have a Changed event (EventHandler) for each property
but it is only used when binding directly to the object and not a list of.

Some relevant code:

public class CustomObject
{
public event EventHandler QuantityChanged;
private int quantity;
internal CustomCollection List;

[System.ComponentModel.Bindable(true)]
public int Quantity
{
 get { return quantity; }
 set
 {
   if (quantity != value)
  {
      quantity = value;
      if (QuantityChanged != null)
        QuantityChanged(this, EventArgs.Empty );
      NotifyList();
  }
 }
 private void NotifyList()
 {
      if ( List!=null )
         List.ItemChanged( this );
  }
}

//
//  incomplete, only relevant parts are shown
//
public class CustomCollection : CollectionBase, IBindingList
{
  public void Add( CustomObject o )
  {
     List.Add( o );
  }

  protected virtual void OnInsert( int index,  object value )
  {
     CustomObject o =  value as CustomObject;
     if ( o == null )
        throw new Exception("Insert CustomObjects only");
     else
       o.List = this;
  }

  protected virtual void OnInsertComplete( int index, object value )
 {
     OnListChanged( new  ListChangedEventArgs(
             ListChangedType.ItemAdded, index ) );
  }

  internal void ItemChanged( CustomObject o )
  {
        OnListChanged( new ListChangedEventArgs(
                ListChangedType.ItemChanged,
                List.IndexOf( o ) ));
      }
  }

  protected virutal OnListChanged( ListChangedEventArgs e )
  {
      if ( ListChanged!=null )
         ListChanged( this, e );
   }
}

HTH,
Greetings

> I know that I can do something like that, but I want nicer solution
> (pseudo
[quoted text clipped - 13 lines]
>
> Sanjin
Sanjin - 12 Oct 2005 13:40 GMT
Thanks, that was what I was looking for.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.