Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
I have gridview control with template field
I just want to change Header text of particular TemplateField on event of Button Click....

Any Idea....?


Thnx in Advance :-)
Posted
Updated 15-Feb-17 3:51am

try this..:)

C#
protected void Button1_Click(object sender, EventArgs e)
{
    this.gvw1.Columns[ColumnIndex].HeaderText = "The new header";
}
 
Share this answer
 
Comments
Member 7839972 6-Jul-16 19:27pm    
ñ{{ñ{ñ
Hi Try this sample..
columnIndex: column which you want to change the name


XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery.js.js"></script>
    <script type="text/javascript">

        var changeheader = function (columnIndex) {

            var grid = document.getElementById('<%= gv.ClientID %>');
            var headerCell = grid.rows[0].cells[columnIndex];
            headerCell.innerText = 'changed value';
            debugger;

        }
    </script>


</head>
<body>
    <form id="form1" runat="server">
        <asp:Button ID="btnSearch" Text="search" runat="server" OnClientClick="changeheader(0); return false;" />
        <br />
        <asp:GridView ID="gv" AutoGenerateColumns="false" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Eval("name")%>'>
                        </asp:Label>
                    </ItemTemplate>
                    <HeaderTemplate>
                        Header Name
                    </HeaderTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


    </form>
</body>
</html>
 
Share this answer
 
Hi Please try it:
C#
protected void Button1_Click(object sender, EventArgs e)
{
   
if(GridView1.Rows.Count>0)
{
    GridView1.HeaderRow.Cells[0].Text="something";
}

}


Yours Farhad.
 
Share this answer
 
v2
Comments
Member 3737803 21-Nov-14 11:22am    
solution 2 removes the post function if the headers have sort functionality on click of header

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900