ASP.NET Dynamically change GridView Header Text

今天公司需要更改公司的網站上GridView Header上的 內容.

之前以為解決方法很簡單的..
只是在DataBinding
使用更改Header 的Code 便可
E.G.
VB.Net

ShareChiWai_GridView1.Columns(1).HeaderText="New Column Header Content"

C#

ShareChiWai_GridView1.Columns[1].HeaderText="New Column Header Content";

可惜不能成功..

最後終於找到了解決放法了…
我們需要建立一個GridView RowCreated Event
之後檢查現在的Row類型是不是 DataControlRowType.Header

如果是的話便使用以下的Code 來更改內容..
e.Row.Cells(ColumnID).Text =”新的Column Header”

解決方法:

VB.Net

Protected Sub gv_ShareChiWai_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_ShareChiWai.RowCreated
If (e.Row.RowType = DataControlRowType.Header) Then
e.Row.Cells(6).Text = "中文內容"
e.Row.Cells(8).Text = "第8行"
End If
End Sub

C#

protected void gv_ShareChiWai_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
	if ((e.Row.RowType == DataControlRowType.Header)) {
		e.Row.Cells[6].Text = "中文內容";
		e.Row.Cells[8].Text = "第8行";
	}
}

Hope you find it useful

 

Leave a Reply