问题描述
我有一个简单的 List
,我希望它显示在 DataGridView
列中.
如果列表将包含更复杂的对象,只需将列表建立为其 DataSource
属性的值.
I have a simple List<string>
and I'd like it to be displayed in a DataGridView
column.
If the list would contain more complex objects, simply would establish the list as the value of its DataSource
property.
但是这样做时:
myDataGridView.DataSource = myStringList;
我得到一个名为 Length
的列,并显示字符串的长度.
I get a column called Length
and the strings' lengths are displayed.
如何在列中显示列表中的实际字符串值?
How to display the actual string values from the list in a column?
推荐答案
那是因为 DataGridView 查找包含对象的属性.对于字符串,只有一个属性——长度.所以,你需要一个像这样的字符串的包装器
Thats because DataGridView looks for properties of containing objects. For string there is just one property - length. So, you need a wrapper for a string like this
public class StringValue
{
public StringValue(string s)
{
_value = s;
}
public string Value { get { return _value; } set { _value = value; } }
string _value;
}
然后将 List
对象绑定到您的网格.它有效
Then bind List<StringValue>
object to your grid. It works
这篇关于如何绑定 List<string>到 DataGridView 控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论