2015-10-24 46 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"; 
     } 
    }