DataGridView中的DataSource

时间:2011-04-05 10:52:06

标签: winforms datagridview

我这个代码

public class Test
         {
             public string name;
             public int age;

             public Test (string name, int age)
             {
                 this.name = name;
                 this.age = age;
             }
         }

         private void button1_Click (object sender, EventArgs e)
         {
             List <Test> listTest = new List <Test> ();
             listTest.Add (new Test ("Pavel", 30));
             listTest.Add (new Test ("Dima", 48));
             listTest.Add (new Test ("Vova", 48));
             dataGridView1.DataSource = listTest;
         }

DataGridView显示三行,但没有值不告诉我错误

1 个答案:

答案 0 :(得分:1)

尝试将名称和年龄作为属性。它会解决你的问题。

public class Test
    {
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }

        public Test(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
    }

希望您使用.Net 3.5或更高版本,否则自动属性不起作用。

这是截图

enter image description here