如何绑定列表<串GT;到DataGridView控件? [英] How to bind a List<string> to a DataGridView control?

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

问题描述

我有一个简单的列表<串> ,我想它显示在 DataGridView的列。结果
如果该列表将包含更复杂的对象,简单地将建立列表为数据源属性的值。

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;

我得到一个名为长度列,并显示字符串的长度。

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;
}

然后绑定列表&LT;&的StringValue GT; 对象网格。它的工作原理

Then bind List<StringValue> object to your grid. It works

这篇关于如何绑定列表&LT;串GT;到DataGridView控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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