gpt4 book ai didi

c# - 如何在 C# 中将数据源分配给 ListBox 时从 ListBox 中删除选定的项目?

转载 作者:太空狗 更新时间:2023-10-29 22:21:11 24 4
gpt4 key购买 nike

如何在 C# 中将数据源分配给 ListBox 时从 ListBox 中删除选定的项目?

尝试删除时出现错误

“设置 DataSource 属性后无法修改项目集合。”


但是当我尝试从数据源(数据表)中删除项目时,

它引发错误,因为“数据行不在当前行集合中”。

最佳答案

在 DataSource 对象中找到该项目并将其删除,然后重新绑定(bind) ListBox。

编辑:

无论 .NET 版本如何,这里都是从 DataTable 中删除数据源的方法。

DataRowView rowView = listBox.SelectedItem as DataRowView;

if (null == rowView)
{
return;
}

dt.Rows.Remove(rowView.Row);

除了 WinForms DataGridViews,我没有尝试过其他任何东西,但我强烈推荐 BindingListView ,它比 DataTables/Views 更快,并且允许您将通用 List 绑定(bind)为数据源。

关于c# - 如何在 C# 中将数据源分配给 ListBox 时从 ListBox 中删除选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/709562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com