在ASP.net 的Gridview控件上动态绑定数据的问题,请教!!!
我的代码是这样的:
string startupPath = Server.MapPath( "\\第一个ASP.NET1\\App_Data\\CBFADB.mdb ");
con = new OleDbConnection(@ "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "
+ startupPath + ";Jet OLEDB:Database Password=cbfadb ");
con.Open();
adapter = new OleDbDataAdapter( "select * from capitalcard ", con);
ds = new DataSet( "select * from capitalcard ");
adapter.Fill(ds);
BoundField aFild = new BoundField();
aFild.DataField = ds.Tables[0].Columns[ "code "].ToString();
aFild.HeaderText = "资产编号 ";
GridView1.Columns.Add(aFild);
BoundField bFild = new BoundField();
bFild.DataField = ds.Tables[0].Columns[ "name "].ToString();
bFild.HeaderText = "资产名称 ";
GridView1.Columns.Add(bFild);
this.GridView1.Columns.Clear();
GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
资产编号和资产名称的列可以显示出来,但表中的其它的字段也显示出来了,可我并不想要.
------解决方案--------------------GridView1.AutoGenerateColumns = false;
this.GridView1.Columns.Clear()
BoundField aFild = new BoundField();
aFild.DataField = "code ";
aFild.HeaderText = "资产编号 ";
GridView1.Columns.Add(aFild);
BoundField bFild = new BoundField();
bFild.DataField = "name ";
bFild.HeaderText = "资产名称 ";
GridView1.Columns.Add(bFild);
;
GridView1.DataSource = ds;
this.GridView1.DataBind();