ASP.NET:何时以及如何在代码中动态更改Gridview的headerText?

时间:2011-06-10 01:06:48

标签: c# asp.net gridview

我有一个包含2列的gridview。我想学习编码,不想在aspx文件中执行此操作。 如何动态设置列的标题文本?我什么时候这样做?适配器在使用数据填充gridview之后? 现在,我有标题文本,但它与数据字段名称是last_name完全相同,我想在标题字段中看到姓氏。 我试过了

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>

4 个答案:

答案 0 :(得分:14)

尝试将其放入GridView1.RowDataBound处理程序中。 评估e.Row.RowType以确定它是否是标题行,然后替换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"

答案 1 :(得分:2)

添加到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";
}

答案 2 :(得分:1)

我认为您不希望在网格中的每一行数据绑定事件(每行一次)中绑定标题的文本!

只需挂钩页面的Loaded事件,然后像你一样将文本绑定在那里。

 protected void Page_Load(object sender, EventArgs e)
 {
    GridView1.Columns[0].HeaderText = "Last Name";
 }

答案 3 :(得分:0)

     <%@ 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;

        }

    }



}