2015-10-24 106 views
-1
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的代码,但列名跟随数据库,我该如何更改列名?如何更改GridView的列名称

+0

[常见问题]和[提问] – MickyD

+0

阅读这篇文章[如何对收口的GridView与 - DataReader的功能于ASPNET-使用-C-和VBNet] (http://www.aspsnippets.com/Articles/How-to-bind-GridView-with-DataReader-in-ASPNet-using-C-and-VBNet.aspx),我假设你想改变'HeaderText'属性GridView'也在那里演示(ContactName为Contac ** t N ** ame) – haraman

回答

0

使用查看的datagridview而不是显示直接,这将节省您的各种问题,按照这个link

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"; 
     } 
    }