4
votes

I am using a class called SimpleFilteredList which I got from this site:

http://blogs.msdn.com/b/winformsue/archive/2007/12/06/filtering-code.aspx

It allows me to apply basic sorting to business objects when added to a DataGridView through a BindgingSource. It has served my purposes very well, however I don't understand one aspect.

Each time a new row is selected in the DataGridView this prompts the overridden EndNew function in the SimpleFilteredList class to be called. This is particularly annoying when the last row was the previous row selected because it forces the sorting algorithm to be executed.

All the columns and the DataGridView have Readonly set to True, and AllowUserToAddRows and AllowUserToDeleteRows are set to False.

How can I stop this EndNew function being called when a new row is selected in the DataGridView?

EndNew function in SimpleFilteredList Class:

    public override void EndNew(int itemIndex)
    {
        // Check to see if the item is added to the end of the list,
        // and if so, re-sort the list.
        if (sortPropertyValue != null && itemIndex > 0 && itemIndex == this.Count - 1)
            ApplySortCore(this.sortPropertyValue, this.sortDirectionValue);
        base.EndNew(itemIndex);
    }
1
You can not just post the whole solution for us to read!, add only the code that is relevant...Jalal Said
I have removed the form code from the post. I originally posted it so that it may be easier for someone to reproduce the problem.ChrisAU
@MrCritter: Still way too much code.leppie
ok, I've reduced the code even further, to only reference the function of concern. I hope this is enough information.ChrisAU

1 Answers

1
votes

Check the index of the item and ignore the call if it's not specified.

Note In some scenarios, such as Windows Forms complex data binding, the collection may receive CancelNew or EndNew calls for items other than the newly added item. (Each item is typically a row in a data view.) Ignore these calls; cancel or commit the new item only when that item's index is specified.

http://msdn.microsoft.com/en-us/library/system.componentmodel.icanceladdnew.aspx