How do I bind the result of DataTable.Select() to a ListBox control?(如何将 DataTable.Select() 的结果绑定到 ListBox 控件?)
问题描述
我有以下代码:
ListBox.DataSource = DataSet.Tables("table_name").Select("some_criteria = match")
ListBox.DisplayMember = "name"
DataTable.Select()
方法 返回 System.Data.DataRow
对象.
The DataTable.Select()
method returns an array of System.Data.DataRow
objects.
无论我在 ListBox.DisplayMember
属性中指定什么,我所看到的只是具有正确数量的项目的 ListBox 全部显示为 System.Data.DataRow
我想要的值在 "name"
列中!
No matter what I specify in the ListBox.DisplayMember
property, all I see is the ListBox with the correct number of items all showing as System.Data.DataRow
instead of the value I want which is in the "name"
column!
是否可以从 DataTable.Select()
绑定到结果数组,而不是循环遍历它并将每个数组添加到 ListBox
?
Is it possible to bind to the resulting array from DataTable.Select()
, instead of looping through it and adding each one to the ListBox
?
(我对循环没有问题,但似乎不是一个优雅的结局!)
(I've no problem with looping, but doesn't seem an elegant ending!)
推荐答案
使用 DataView 代替.
ListBox.DataSource = new DataView(DataSet.Tables("table_name"), "some_criteria = match", "name", DataViewRowState.CurrentRows);
ListBox.DisplayMember = "name"
这篇关于如何将 DataTable.Select() 的结果绑定到 ListBox 控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 DataTable.Select() 的结果绑定到 ListBox 控件?


- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01