开发者

DataSource in DataGridView

开发者 https://www.devze.com 2023-02-21 23:04 出处:网络
I this code public class Test { public string name; public int age; public Test (string name, int age) { this.name = name;

I this code

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

             public Test (string name, int age)
             {
                 this.name = name;
                 this.age = age;
         开发者_JAVA百科    }
         }

         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;
         }

The DataGridView displays three lines, but no value does not tell me that I had incorrectly


Try making the name and age as properties. It will fix your problem.

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;
        }
    }

Hopes you are using .Net 3.5 or more, otherwise Automatic properties doesn't work.

Here is the screenshot

DataSource in DataGridView

0

精彩评论

暂无评论...
验证码 换一张
取 消