绑定List<>的问题到DataGridView

时间:2015-04-09 08:51:44

标签: c# winforms datagridview

我将列表绑定到datagridview(使用Bindingsource)。 但我的DataGridView永远不会填充。 有什么理由吗?

以下是代码:

  {
        public BindingList<Rat> list = new BindingList<Rat>();
        BindingSource bs;

        public Form1()
        {
            InitializeComponent();

            bs = new BindingSource();
            bs.DataSource = list;

            list.Add(new Rat(12, "Hubert", "cousin"));
            list.Add(new Rat(7, "Joe", "taxi driver"));
            list.Add(new Rat(3, "Bill", "DaBoss"));

            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = bs;
        }

    }

    public class Rat
    {
        public int Age;
        public string Name;
        private string Nickname;

        public Rat(int age, string name, string nick)
        {
            Age = age;
            Name = name;
            Nickname = nick;
        }
    }

提前致谢。

1 个答案:

答案 0 :(得分:2)

尝试将字段更改为Rat

中的属性
public int Age { get; set }
public string Name { get; set }
private string Nickname { get; set }