con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=|DataDirectory|\database.mdb");
com = new OleDbCommand("Select * from testing", con);
con.Open();
OleDbDataReader reader;
reader = com.ExecuteReader();
GridView1.DataMember = "testing";
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Close();
con.Close();
这是查看GridView中所有数据的代码,但列名是跟随数据库的,如何更改列名?
答案 0 :(得分:0)
在 datagridview 中使用查看,而不是直接显示列,这样可以避免各种问题,请按照link进行操作。< / p>
答案 1 :(得分:0)
testdgv.Columns[0].HeaderText = "Date";
或
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Name";
e.Row.Cells[1].Text = "City";
}
}