tags:

views:

512

answers:

1

I have a WinForm containing a DataGridView with a list of members in a group. The form contains functionality (bindingNavigator) for adding new members and changing the role of the current members. I want to be albe to sort the members and implemented this by introducing a SortedBindingList to the mix. However this has the unfortunate effect of denying me the ability to add new member to the team. I get the following exception:

System.InvalidOperationException occurred

Message="Item cannot be added to a read-only or fixed-size list."

I understand the error, but is there any way around this or do I have to find some other way to sort the list?

+3  A: 

I've had a lot of luck using Binding List View. Just keep your objects in Generic lists, and set the DataSource like so:

public void BindGenericList<T>(List<T> list)
{
    DataSource = new BindingListView<T>(list);
}

Getting the actual object back out of the list is as simple as:

public void GetObjectFromRow<T>(int rowIndex)
{
    BindingListView<T> bindingListView = DataSource as BindingListView<T>;

    return (null != bindingListView) ? bindingListView[rowIndex].Object : default(T);
}

Sorting with BLV is actually faster than using a DataView.

Chris Doggett