我写了一个小程序,其中创建了Student实体类代码如下:
class Student
    {
        private string stuId;
        private string stuName;
        private int age;        public Student(string stuId, string stuName, int age)
        {
            this.stuId = stuId;
            this.stuName = stuName;
            this.age = age;
        }        public string StuId
        {
            get { return stuId; }
            set { stuId = value; }
        }
        public string StuName
        {
            get { return stuName; }
            set { stuName = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
在界面当中我这样设置DataGridView的数据源:
List<Student> lstStu = new List<Student>();
lstStu.Add(new Student("001", "张三", 22));
lstStu.Add(new Student("002", "李四", 23));
lstStu.Add(new Student("003", "王五", 24));
lstStu.Add(new Student("004", "赵六", 25));
dataGridView1.AutoGenerateColumns = true;我预期准备让DataGridView的第一列显示学号,第二列显示型名,第三列显示年龄。就跟实例化Student时一样。
但是问题来了,显示的结果却不和我想象的一样,结果为姓名第一列,学号第二列,年龄第三列。我的目的就是想
让其显示的顺序与构造Student时的顺序一样。这怎么做呢?求大虾帮忙了,谢谢阿。
            dataGridView1.DataSource = lstStu;

解决方案 »

  1.   

    我觉得可以先用lstStu生成一个你想要的DataTable,
    然后再dataGridView1.DataSource   =datatable;
      

  2.   

    我也遇到楼主的问题,还未解决.
    不过如果要用到对记录的增删改操作,我想用datatable应该会方便点,dataGridView好像不能直接绑定到List对象吧.
    不知道楼主解决了没有,一起讨论下
      

  3.   

    有的时候给dataGridView1.DataSource赋值时要先清一下dataGridView1.DataSource=null;
    然后再赋值,不然列的顺序会有变化。
      

  4.   

    IList <Student>   lstStu   =   new   List <Student> (); 
      

  5.   

    List <Student>   lstStu   =   new   List <Student> (); 
                lstStu.Add(new   Student("001",   "张三",   22)); 
                lstStu.Add(new   Student("002",   "李四",   23)); 
                lstStu.Add(new   Student("003",   "王五",   24)); 
                lstStu.Add(new   Student("004",   "赵六",   25));
                dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.Columns.Clear();
                int colIndex;
               colIndex = dataGridView1.Columns.Add("StuId", "StuId");
               dataGridView1.Columns[colIndex].DataPropertyName = "StuId";
               colIndex = dataGridView1.Columns.Add("StuName", "StuName");
               dataGridView1.Columns[colIndex].DataPropertyName = "StuName";
               colIndex = dataGridView1.Columns.Add("Age", "Age");
               dataGridView1.Columns[colIndex].DataPropertyName = "Age";           
                dataGridView1.DataSource = lstStu.ToArray();
      

  6.   

    完全可以放到datatable中  然后绑定
      

  7.   

    你采用了默认的表结构,解决的方法是你用一个自定义的TABLE结构!