DataGridView 绑定List时 属性不显示的解决方法

DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();            

col1.HeaderText = "姓名";            

col1.DataPropertyName = "Name";            

col1.Name = "Name";                        

col1.Width = 100;            

DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();            

col2.HeaderText = "性别";            

col2.DataPropertyName = "Sex";            

col2.Name = "Sex";            

col2.Width = 80;

this.dataGridView1.Columns.Add(col1);            

this.dataGridView1.Columns.Add(col2);

 

IList<User> list = new List<User>();            

list.Add(new User("wtq", "男"));            

list.Add(new User("wtm", "男"));            

list.Add(new User("wts", "男"));

this.dataGridView1.DataSource = list;

// User 类

public class User
{
        public  User(string userName,string sex)
        {
            this.Name = userName;
            this.Sex = sex;
        }

    /*注意必须要写成如此形式才能显示,写成public string Name; DataGridView里面不显示*/
        public string Name  {get;set;}
        public string Sex   {get;set;}
}

posted @ 2012-07-31 13:27  oftenlin  阅读(2578)  评论(0编辑  收藏  举报