.NET / C# 绑定 IList到 DataGridView

发布于 2024-07-27 15:35:19 字数 200 浏览 3 评论 0原文

我有一个从函数(作为变量 lst)返回的 IList ,我进行了设置,然后我的

this.dataGridView1.DataSource = lst;

数据网格添加了一列标记为 Length 的列,然后列出了每个字符串的长度。 如何让它只列出字符串?

I have an IList<string> returning from a function (as variable lst) and I set and then I

this.dataGridView1.DataSource = lst;

The datagrid adds one column labelled Length and then lists the length of each string.
How do I make it just list the strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山有枢 2024-08-03 15:35:19

您确实需要一个具有字符串属性的对象列表。 使用.NET 3.5,您可以作弊:

.DataSource = list.Select(x=>new {Value = x}).ToList();

否则创建一个虚拟类并手动复制数据...

You really need a list of objects that have a string property. With .NET 3.5 you could cheat:

.DataSource = list.Select(x=>new {Value = x}).ToList();

Otherwise create a dummy class and copy the data in manually...

怪我入戏太深 2024-08-03 15:35:19

这是因为 DataGridView 显示对象的属性。 在这种情况下,列表只有一个属性“长度”,因此它只能显示“长度”(无论数据类型如何)。
您需要创建一个包装类来实现您想要的功能(一个具有“Text”属性的“MyString”类,然后在网格中显示一个列表)。

希望这有助于

添加代码示例

 class MyString
    {
        private string _text;
        public string Text
        { get 
             { 
              return _text; 
             }
            set 
            {
                _text = value; 
            }
        }

    }

在执行表单中

 private List<MyString> foo()
        {
            List<MyString> lst = new List<MyString>();
            MyString one = new MyString();
            MyString two = new MyString();
            one.Text = "Hello";
            two.Text = "Goodbye";
            lst.Add(one);
            lst.Add(two);
            return lst;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = foo();

        }

This is because DataGridViews show properties of the object. In this case the List only has one property "Length", therefore it can only display "Lenght" (regardless of DataType).
You need to create a wrapper class to achieve what you want (a "MyString" class with a property of "Text", then have a List displayed in your grid).

Hope this helps

Adding Code Example

 class MyString
    {
        private string _text;
        public string Text
        { get 
             { 
              return _text; 
             }
            set 
            {
                _text = value; 
            }
        }

    }

'In the executing form

 private List<MyString> foo()
        {
            List<MyString> lst = new List<MyString>();
            MyString one = new MyString();
            MyString two = new MyString();
            one.Text = "Hello";
            two.Text = "Goodbye";
            lst.Add(one);
            lst.Add(two);
            return lst;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = foo();

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文