微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – DataGridView对EndNew的不必要调用

我正在使用一个名为SimpleFilteredList的类,我从这个站点获得:

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

它允许我在通过BindgingSource添加到DataGridView时对业务对象应用基本排序.它很好地服务于我的目的,但我不明白一个方面.

每次在DataGridView中选择一个新行时,都会提示调用SimpleFilteredList类中的重写EndNew函数.当最后一行是选择的前一行时,这尤其令人讨厌,因为它强制执行排序算法.

所有列和DataGridView都将Readonly设置为True,AllowUserToAddRows和AllowUserToDeleteRows设置为False.

在DataGridView中选择新行时,如何停止调用此EndNew函数

SimpleFilteredList类中的EndNew函数

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);
    }

解决方法

检查项目的索引,如果未指定,则忽略该调用.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐