private DataTable dt = new DataTable();
BindingSource bs = new BindingSource();
/// <summary>
/// 初始化DataTable
/// </summary>
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"; }