DevExpress学习之ASPxGridView后台增加列
public void BindRecordList()
{
String sqlAll=”select ID,”;
if (ASPxGridView1.Columns.Count == 0)
{
//手动绑定数据
DevExpress.Web.ASPxGridView.GridViewDataTextColumn dl = newDevExpress.Web.ASPxGridView.GridViewDataTextColumn();
dl.Caption = "显示出来的头部";
dl.FieldName = "AgentId";
gvRecordList.Columns.Add(dl);
//从数据库中取的要绑定的列名与描述
DataTable dsWidth = gd.getGradeReg();
String Sqlcolumn=””;
for (int i = 0; i < dsWidth.Rows.Count; i++)
{
DevExpress.Web.ASPxGridView.GridViewDataTextColumn datacolumn = newDevExpress.Web.ASPxGridView.GridViewDataTextColumn();
//绑定列的描述
datacolumn.Caption = dsWidth.Rows[i]["GrShow"].ToString();
//绑定列的字段
datacolumn.FieldName = dsWidth.Rows[i]["GrName"].ToString().Trim();
datacolumn.UnboundType = DevExpress.Data.UnboundColumnType.Bound;
ASPxGridView1.Columns.Add(datacolumn);
//取得sql语句中的字段
Sqlcolumn+=” , “+ dsWidth.Rows[i]["GrName"].ToString().Trim()
}
}
DataSet dsRecord = null;
try
{//取得完整的sql语句
sqlAll += ColonmFileds + " from Recording " + sqlQuery();
dsRecord = SQLHelper.Query(sqlAll);
//绑定数据源
ASPxGridView1.DataSource = dsRecord.Tables[0].DefaultView;
ASPxGridView1.DataBind();
}
catch
{
throw;
}
}
GridView在后台动态绑定列或者增加列的语句为:
for (int i = 0; i < dsWidth.Rows.Count; i++)
{
BoundField bf = new BoundField();
bf.DataField = dsWidth.Rows[i]["GrName"].ToString().Trim();//字段名
bf.HeaderText = dsWidth.Rows[i]["GrShow"].ToString();//列名
GridView1.Columns.Insert(i, bf);
}