BandingList 泛型集合数据绑定

  public IList<Student> IStudent = new List<Student>(); 
  public BindingList<Student> BStudent = new BindingList<Student>();

  /// <summary>
  /// 加载数据返回IList
  /// </summary>

  IList<Student> BindDataList(DataTable dt)
  {
    IStudent.Clear();
    foreach (DataRow row in dt.Rows)
    {
      Student student = new Student();
      student.Name = row["name"].ToString();
      student.Sex = row["sex"].ToString();
      IStudent.Add(student);
    }
    IStudent.Add(new Student() { Name = "2", Sex = "3" });
    return IStudent;
  }

  /// <summary>
  /// 绑定数据
  /// </summary>

  public void Loading()
  {
    BStudent = new BindingList<Student>(BindDataList(dt));
    gridControl1.DataSource = BStudent;
  }

  /// <summary>
  /// 实体类
  /// </summary>

  public class Student
  {
    public string Name { get; set; }

    public string Sex { get; set; }
  }

 

posted @ 2019-09-29 15:31  枫卍  阅读(266)  评论(0编辑  收藏  举报