如何绑定 List<string>到 DataGridView 控件?

编程入门 行业动态 更新时间:2024-10-26 06:32:38
本文介绍了如何绑定 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 10:54:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/753664.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   控件   List   string   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!