如何绑定List< T>的DataGridView或BindingList< T> [英] How to bind DataGridView with List<T> or BindingList<T>

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

问题描述

我已经做了一千次,它的工作,但现在....不:(

I've done it one thousand of times and it works but now .... not :(

我在这里做错了,因为没有显示网格?

Am I doing something wrong here because nothing is shown in grid ?

namespace theGridIsNotWorking
{
using System;
using System.Collections.Generic;
using System.Windows.Forms;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var items = new List<Item>();

        items.Add(new Item(){ TheName = "first"});
        items.Add(new Item(){ TheName = "Second"});
        items.Add(new Item(){ TheName = "Third"});

        dataGridView1.DataSource = new List<Item>(items);
    }

    public class Item
    {
        public string TheName;
    }
}
}

没有什么乐谱.... b真的很难过。

Nothing spectaculos .... but really sad.

推荐答案

我认为问题是TheName是一个成员变量,但是你需要一个属性。
尝试以下项目类:

I think the problem is that TheName is a member variable, but you need a property. Try the following for the Item class:


      public class Item
      {
         public string TheName;

         public string TheNameProperty
         {
            get
            {
               return TheName;
            }
         }

         public Item(string name)
         {
            TheName = name;
         }
      }

这篇关于如何绑定List&lt; T&gt;的DataGridView或BindingList&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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