【问题标题】:ASP.NET: When and how to dynamically change Gridview's headerText's in code behind?ASP.NET:何时以及如何在代码后面动态更改 Gridview 标题文本?
【发布时间】:2011-06-10 01:06:47
【问题描述】:

我有一个 2 列的网格视图。我想学习后面的编码并且不想在 aspx 文件中这样做。 如何动态设置列的标题文本?我在什么时候这样做?在适配器用数据填充网格视图之后? 现在,我有标题文本,但它与 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>

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    尝试将其放入 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"
    

    【讨论】:

    • 您介意提供更多有关此的信息吗?我也很好奇答案。也许有点代码?
    • 非常感谢,乔。但是在我设法更改标题文本之后:我通过单击列的标题失去了排序能力。如果您没有立即得到答案,我可以单独发布一个问题。再次感谢。
    • 查看更新的示例。实际上,使用 HeaderText 属性比我最初建议的要好。
    • 实际上,乔,原来的工作。但是更新的代码没有。请改回代码以使其成为正确答案。谢谢。
    • 第二种方法在我的测试中肯定有效(重命名和排序),所以还有别的东西在起作用。您收到什么错误消息,仍然“索引超出范围”?你能发布 GridView 的代码吗?
    【解决方案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";
    }
    

    【讨论】:

      【解决方案3】:

      我认为您不想在网格中的每个行数据绑定事件(每行 1 次)期间为标题绑定文本!

      只需连接到页面的 Loaded 事件,然后像之前那样绑定文本。

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

      【讨论】:

        【解决方案4】:
             <%@ 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;
        
                }
        
            }
        
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          相关资源
          最近更新 更多