创建DataTable与DataGridView进行绑定

2021-07-14 18:13

阅读:496

标签:this   自动   property   界面   sum   rop   als   tab   idv   

private DataTable dt = new DataTable();

BindingSource bs = new BindingSource();

 

///


/// 初始化DataTable
///

public void InitDataTable()

//不允许自动生成,若改为允许,界面会自动增加DataTable列,那么界面上既会包含DataGridView中定义的列,也会包含DataTable定义的列
this.dataGridView1.AutoGenerateColumns = false;

DataColumn col = new DataColumn("No", typeof(int));
dt.Columns.Add(col);
dt.Columns.Add(new DataColumn("Addr", typeof(string)));
dt.Columns.Add(new DataColumn("FuntionType", typeof(string)));
dt.Columns.Add(new DataColumn("Result", typeof(string)));

bs.DataSource = dt;
this.dataGridView1.DataSource = bs;

 

//将DataGridView中的列与DataTable中的列进行数据绑定,this.cloNum为列名

this.colNum.DataPropertyName = "No";
this.colAddress.DataPropertyName = "Addr";
this.colFunction.DataPropertyName = "FuntionType";
this.colResult.DataPropertyName = "Result";
}

创建DataTable与DataGridView进行绑定

标签:this   自动   property   界面   sum   rop   als   tab   idv   

原文地址:https://www.cnblogs.com/zst-blogs/p/9534220.html


评论


亲,登录后才可以留言!