[英]ASP.NET: When and how to dynamically change Gridview's headerText's in code behind?
我有一個帶有 2 列的 gridview。 我想學習后面的編碼,不想在 aspx 文件中這樣做。 如何為我的列動態設置 header 文本? 我在什么時候這樣做? 在適配器用數據填充 gridview 之后? 現在,我有 header 文本,但它與 last_name 的數據字段名稱完全相同,我想在 header 字段中查看姓氏。 我試過了
GridView1.Columns[0].HeaderText = "Last Name";
但無論我試圖把它放在哪里,編譯器都會抱怨索引超出范圍。
謝謝。
gridview 的 aspx 代碼:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
Width="728px" CellPadding="4" ForeColor="Black" GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging"
OnSorting="GridView1_Sorting" PageSize="14" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last"
PageButtonCount="5" Position="Bottom" />
</asp:GridView>
嘗試將其放入 GridView1.RowDataBound 處理程序中。 評估 e.Row.RowType 以確定它是否是 header 行,然后替換 HeaderText。
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) {
GridView1.Columns[0].HeaderText = "Last Name";
}
}
但是,如果您正在動態創建列並使用排序,則需要以這種方式處理它,以防止意外將鏈接轉換為純文本:
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) {
LinkButton HLink = (LinkButton)e.Row.Cells[0].Controls[0];
HLink.Text = "Last Name";
}
}
使用任一方法,將此屬性添加到 ASPX 中的 Gridview 中:
OnRowDataBound="GridView1_RowDataBound"
添加到 Page_Load,但是
GridView1.Columns[0].HeaderText = "Last Name";
不起作用,因為它會抱怨列數為 0,因此這樣做:
protected void grdProd_Load(object sender, EventArgs e)
{
grdProd.HeaderRow.Cells[0].Text = "Item";
grdProd.HeaderRow.Cells[1].Text = "Category";
}
我認為您不想在網格中的每個行數據綁定事件(每行 1 次)期間為 header 綁定文本!
只需連接到頁面的 Loaded 事件,然后將文本綁定到那里,就像你擁有它一樣。
protected void Page_Load(object sender, EventArgs e)
{
GridView1.Columns[0].HeaderText = "Last Name";
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="grdvw8.aspx.cs" Inherits="grdvw8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
first header name change To<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<br />
Second header name change To<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnChange" Text="Change Header Text" runat="server" onclick="btnChange_Click" />
<asp:GridView ID="grdvw" runat="server">
<HeaderStyle Font-Bold="true" ForeColor="Brown" />
</asp:GridView>
</div>
</form>
</body>
</html>
/ASPX.CS PAGE/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class grdvw8 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["code"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
protected void Bind()
{
con.Open();
SqlCommand cmd=new SqlCommand("select * from gridview",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
grdvw.DataSource = ds;
grdvw.DataBind();
}
protected void btnChange_Click(object sender, EventArgs e)
{
if (grdvw.Rows.Count > 0)
{
grdvw.HeaderRow.Cells[0].Text = txt1.Text;
grdvw.HeaderRow.Cells[1].Text = txt2.Text;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.