如何绑定 List<string>到 DataGridView 控件? [英] How to bind a List&lt;string&gt; to a DataGridView control?

查看:34
本文介绍了如何绑定 List<string>到 DataGridView 控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆