将对象添加到 SortedBindingList

发布于 2024-07-13 13:23:23 字数 357 浏览 4 评论 0原文

我有一个 WinForm,其中包含一个带有组中成员列表的 DataGridView。 该表单包含用于添加新成员和更改当前成员角色的功能(绑定导航器)。 我希望能够对成员进行排序,并通过在组合中引入 SortedBindingList 来实现这一点。 然而,这会产生不幸的结果,使我无法向团队添加新成员。 我得到以下异常:

发生System.InvalidOperationException

Message="无法将项目添加到只读或固定大小列表。"

我理解这个错误,但是有什么办法可以解决这个问题,还是我必须找到其他方法来对列表进行排序?

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 able 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-07-20 13:23:23

我很幸运地使用了绑定列表视图。 只需将对象保留在通用列表中,然后像这样设置数据源:

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

将实际对象从列表中取出就像这样简单:

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

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

使用 BLV 排序实际上是 比使用 DataView 更快

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文