.net 如何将数据绑定到列表视图中的多列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/267410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you bind data to multiple columns in a list view?
提问by wusher
I have a datasource that I want to bind to a listview that has multiple columns. How do I bind my datasource to that listview
我有一个数据源,我想绑定到具有多列的列表视图。如何将我的数据源绑定到该列表视图
Here is some pseudo code that doesn't work to help illustrate what I am trying to do:
这是一些无法帮助说明我想要做的事情的伪代码:
MyDataTable dt = GetDataSource();
ListView1.DataBindings.Add("Column1.Text", dt, "MyDBCol1");
ListView1.DataBindings.Add("Column2.Text", dt, "MyDBCol2");
-- edit --
- 编辑 -
Sorry, I forgot to mention it was winforms.
对不起,我忘了提到它是winforms。
采纳答案by Alexander Prokofyev
It seems there is a lacuna in functionality of WinForms ListView control (thought it's possible to databindnew WPF ListBox).
WinForms ListView 控件的功能似乎存在缺陷(认为可以对新的 WPF ListBox进行数据绑定)。
This article by Nick Karnik describes how to add databinding capability to custom control inherited from WinForms ListView - Data binding a ListView.
Nick Karnik 的这篇文章描述了如何向从 WinForms ListView 继承的自定义控件添加数据绑定功能 -数据绑定一个 ListView。
回答by Peter Gfader
Listview has no Datasource, Items must be added manually.
Listview 没有数据源,必须手动添加项目。
I would suggest to use the DatagridView instead of the Listview.
Particularly if you have a lot of items.
我建议使用 DatagridView 而不是 Listview。
特别是如果你有很多物品。
回答by Marc Gravell
Can you please clarify whether this is winforms vs webforms? Both have a ListView. From the DataBindings.AddI'm assuming winforms.
你能澄清一下这是winforms还是webforms吗?两者都有一个ListView. 从DataBindings.Add我假设winforms。
Would DataGridViewbe a pragmatic option? This will support multi-column binding out of the box?
会DataGridView是一个务实的选择吗?这将支持开箱即用的多列绑定?
回答by Handruin
Check out this reference on binding datasource to listview. Is that what you were looking for?
查看有关将数据源绑定到 listview 的参考。这就是你要找的吗?

