In the collection class, I am overriding the OnInsertComplete method and
raising the ListChanged event, with proper args.

Although, the entire code was not required still I removed certain not
required methods and am posting the code. I have created custom objects for
holding the data and the fields are marked with custom attributes. Code goes
her.....

using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.Remoting;

namespace BusinessEntity
{
       public abstract class BECollection : CollectionBase, ITypedList,
IBindingList
       {
               protected Type _containedType;

               public BECollection( Type t )
               {
                       this._containedType = t;
               }

               virtual internal int Add( BEBase obj )
               {
                       return this.InnerList.Add(obj);
               }

               virtual internal void Remove( BEBase obj )
               {
                       this.InnerList.Remove(obj);
               }

               public BEBase this[int index]
               {
                       get
                       {
                               return InnerList[index];
                       }
                       set
                       {
                               List[index] = value;
                       }
               }

               PropertyDescriptorCollection ITypedList.GetItemProperties(
PropertyDescriptor[] listAccessors )
               {
                       Type typeOfObject = null;

                       if( (listAccessors==null) || listAccessors.Length==0 )
                       {
                               typeOfObject = _containedType;
                       }
                       return this.GetPropertyDescriptors( typeOfObject );
               }

               string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
               {
                       return "Learning..";
               }

               private PropertyDescriptorCollection GetPropertyDescriptors( Type
typeOfObject )
               {
                       PropertyDescriptorCollection typePropertiesCollection =
TypeDescriptor.GetProperties( typeOfObject );
                       ArrayList propertyDescriptorsToUse = new ArrayList();

                       foreach(PropertyDescriptor property in 
typePropertiesCollection)
                       {
                               BEFieldAttribute befieldAttribute =
                                       
(BEFieldAttribute)property.Attributes[typeof(BEFieldAttribute)];

                               if (befieldAttribute != null)
                               {
                                       // add it, as it has the attribute 
applied to it
                                       propertyDescriptorsToUse.Add(property);
                               }
                       }
                       return new
PropertyDescriptorCollection((PropertyDescriptor[])propertyDescriptorsToUse.ToArray(typeof(PropertyDescriptor)));
               }

               public void AddIndex(PropertyDescriptor property)
               {
                       throw new NotSupportedException();
               }

               public bool AllowNew
               {
                       get
                       {
                               return true;
                       }
               }

               public void ApplySort(PropertyDescriptor property,
System.ComponentModel.ListSortDirection direction)
               {
                       throw new NotSupportedException();
               }

               public PropertyDescriptor SortProperty
               {
                       get
                       {
                               throw new NotSupportedException();
                       }
               }

               public int Find(PropertyDescriptor property, object key)
               {
                       throw new NotSupportedException();
               }

               public bool SupportsSorting
               {
                       get
                       {
                               return false;
                       }
               }

               public bool IsSorted
               {
                       get
                       {
                               throw new NotSupportedException();
                       }
               }

               public bool AllowRemove
               {
                       get
                       {
                               return true;
                       }
               }

               public bool SupportsSearching
               {
                       get
                       {
                               return false;
                       }
               }

               public System.ComponentModel.ListSortDirection SortDirection
               {
                       get
                       {
                               throw new NotSupportedException();
                       }
               }

               public event System.ComponentModel.ListChangedEventHandler 
ListChanged;

               public bool SupportsChangeNotification
               {
                       get
                       {
                               return true;
                       }
               }

               public void RemoveSort()
               {
                       throw new NotSupportedException();
               }

               public object AddNew()
               {
                       object obj = 
Activator.CreateInstance(this._containedType);
                       BEBase be = (BEBase)obj;

                       List.Add(be);

                       return obj;
               }

               public bool AllowEdit
               {
                       get
                       {
                               return true;
                       }
               }

               public void RemoveIndex(PropertyDescriptor property)
               {
                       throw new NotSupportedException();
               }

               protected override void OnInsertComplete(int index, object value)
               {
                       ListChanged(this, new 
ListChangedEventArgs(ListChangedType.ItemAdded,
index));
               }
       }
}


Thanks

Regards,
Girish Jain




From: Frans Bouma <[EMAIL PROTECTED]>
Reply-To: "Discussion of advanced .NET topics."
<ADVANCED-DOTNET@DISCUSS.DEVELOP.COM>
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] CurrencyManager and Data Binding
Date: Fri, 2 Jun 2006 12:31:14 +0200

> I have created a custom collection class which inherits from
> CollectionBase class and implements the IBindingList
> interface. I am having a problem regarding adding a new
> record and form binding. When I add a new record by calling
> the AddNew() method on the CurrencyManager class instance,
> the screen does not get refreshed. When I tried to figure out
> the problem, I found that the position of the CurrnecyManager
> does not change, and it does not fire the PositionChanged
> event. I figured that the
> CurrnecyManager.AddNew() method calls the
> IBindingList.AddNew() and it internally calls the
> ChangeRecordState() method. By looking at the call stack, I
> see that the newPosition argument passed to
> ChangeRecordState() method is correct and it raises the
> CurrentChanged event whereas the PositionChanged event does
> not get fired and the UI is not updated.
>
> When I move the record position by incrementing or
> decrementing the Positon property, the UI controls does get
> updated normally but not in the case of
> AddNew() i.e.
>
> _cm.Position = _cm.Count-1;
>
> also does not work
>
> Your inputs on this would be highly appreciated

        You should raise ListChanged events in your class. You should pass
the index of the item changed/ added/ removed as well as
the type of the change in the eventargs.

                Frans

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to