473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Binding a Collection as a component

I have a collection (PersonCollection inherits collectionbase and implements
ITypedList it's a collection of Person objects) this collection also
implements the IComponent interface, so I can drag the collection from the
toolbar to the form editor. Now the problem is when I add a new property to
the Person class, wich type is another collection (it also inherits
collectionbase and implements ITypedList) when I try to bind the new
collection to a text box or select the datamember property of the grid, it
throws an error "The specified method is not compatible"

I tryed everything and couldn't solve it, please help me.
Dec 27 '05 #1
1 1940
Hi,

"Ruben" <Ru***@discussions.microsoft.com> wrote in message
news:D2**********************************@microsof t.com...
I have a collection (PersonCollection inherits collectionbase and
implements
ITypedList it's a collection of Person objects) this collection also
implements the IComponent interface, so I can drag the collection from the
toolbar to the form editor. Now the problem is when I add a new property
to
the Person class, wich type is another collection (it also inherits
collectionbase and implements ITypedList) when I try to bind the new
collection to a text box or select the datamember property of the grid, it
throws an error "The specified method is not compatible"

I tryed everything and couldn't solve it, please help me.


To get the properties of the items of a child-collection it will ask the
parent-collection for them using ITypedList.GetItemProperties, listAccessors
forms a path of properties to the child-collection.

And for some unknown reason IBindingList.AddNew must also be implemented, on
the other hand a custom collection without IBindingList isn't worth that
much. So if you haven't already you should implement IBindingList.

At the end i posted an example, to get basic support for hierarchical custom
collections both at design and runtime.

You can also find an example here (the ITypedList isn't sufficiently
implemented for hierarchical custom collections):
http://msdn.microsoft.com/msdnmag/is...g/default.aspx

If you tried any of the code and still have problems, then post the code for
your custom item and collection.

And if you are using NET2.0, then use BindingList<T>, this already implement
IBindingList and ITypedList is handled by putting a BindingSource between
the collection and control.

HTH,
Greetings

public class PersonCollection : CollectionBase, ITypedList, IComponent,
IBindingList
{
#region Typed Methods
public int Add(Person p )
{
return List.Add(p);
}

public int IndexOf(Person p )
{
return List.IndexOf(p);
}

public void Remove(Person p)
{
List.Remove(p);
}

public Person this[int i]
{
get { return (Person)List[i]; }
set { List[i] = value; }
}
#endregion

#region CollectionBase overrides & notification
private void OnListChanged(ListChangedType t, int OldIndex, int NewIndex)
{
if ( ListChanged!=null )
ListChanged(this, new ListChangedEventArgs(t, OldIndex, NewIndex));
}

protected override void OnSetComplete(int index, object oldValue, object
newValue)
{
OnListChanged(ListChangedType.ItemChanged, index, index);
}

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

protected override void OnClearComplete()
{
OnListChanged(ListChangedType.Reset, -1, -1);
}

protected override void OnRemoveComplete(int index, object value)
{
OnListChanged(ListChangedType.ItemDeleted, index, index);
}
#endregion

#region ITypedList
public PropertyDescriptorCollection GetItemProperties(
PropertyDescriptor[] listAccessors )
{
if ( listAccessors == null || listAccessors.Length == 0 )
{
PropertyDescriptorCollection pdc =
TypeDescriptor.GetProperties(typeof(Person));
return pdc;
}
else
{
ITypedList childCol = null;
if ( Count > 0 )
childCol = (ITypedList) listAccessors[0].GetValue( List[0] );
else
childCol = (ITypedList)
Activator.CreateInstance(listAccessors[0].PropertyType);

// strip one property
PropertyDescriptor[] listAccessors_1 =
new PropertyDescriptor[listAccessors.Length-1];

Array.Copy( listAccessors, 1, listAccessors_1, 0,
listAccessors_1.Length);

return childCol.GetItemProperties(listAccessors_1);
}
}

public string GetListName( PropertyDescriptor[] listAccessors )
{
if ( listAccessors == null || listAccessors.Length ==0 )
return GetType().Name;
else
return listAccessors[listAccessors.Length-1].PropertyType.Name;
}
#endregion

#region IBindingList Members

public event System.ComponentModel.ListChangedEventHandler ListChanged;

public bool SupportsChangeNotification
{
get { return true; }
}

public bool AllowEdit
{
get { return true; }
}

public bool AllowNew
{
get { return true; }
}

public bool AllowRemove
{
get { return true; }
}

public object AddNew()
{
Person p = new Person("new");
int idx = List.Add(p);
// warning: if Person doesn't implement IEditableObject then you need to
// fire itemadded twice, but its already fired once because of List.Add,
so
// this is the second time, if they do implement IEditableObject then
you need
// to call this when IEditableObject.EndEdit is called on a new item.
OnListChanged(ListChangedType.ItemAdded, idx, idx);
return p;
}

public PropertyDescriptor SortProperty
{
get { return null; }
}

public int Find(PropertyDescriptor property, object key)
{
return 0;
}

public bool SupportsSorting
{
get { return false; }
}

public bool IsSorted
{
get { return false; }
}

public bool SupportsSearching
{
get { return false; }
}

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

public void RemoveSort() { }
public void AddIndex(PropertyDescriptor property) { }
public void ApplySort(PropertyDescriptor property,
System.ComponentModel.ListSortDirection direction) { }
public void RemoveIndex(PropertyDescriptor property) { }

#endregion

#region IComponent Members
private ISite site;

public event System.EventHandler Disposed;

public ISite Site
{
get { return site; }
set { site = value; }
}

#endregion

#region IDisposable Members
public void Dispose()
{
if (Disposed!=null) Disposed(this, EventArgs.Empty);
}
#endregion
}

Dec 27 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
0
by: Jason | last post by:
What interface do I need to implement in order to get a component with a public property to show up in the data binding drop down boxes at design time in VS.net? I've tried to look just about...
6
by: GingerNinja | last post by:
Hi Everybody, its about my 4th day on C# and all seems to be going smoothly however I've started to get into Datagrids and in particular binding data sources to them. In the documentation it says I...
1
by: ogs | last post by:
I need to create component that allows binding to it's properties. Maybe there exists some interface or something. Like: =================================================== public class Cat :...
2
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working...
0
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
2
by: Paul | last post by:
Hello All, I am teaching myself C#. The book I am using has the following example using a BindingSource object. Here is the code. NorthwindDataSetTableAdapters.ProductsTableAdapter productsTA...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.