emirate
emirate

Reputation: 159

DataSource in DataGridView

I this code

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

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

Upvotes: 0

Views: 2172

Answers (1)

Anuraj
Anuraj

Reputation: 19618

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

enter image description here

Upvotes: 1

Related Questions