Search This Blog

Saturday, October 27, 2012

Change Gridview header text dynamically at runtime


In this article I have explained, how to change gridview header text dynamically at runtime using asp.net. Many times I can see in the forums people asking for this. So, I posted this scenario here with an example code. We can do this using the GridView’s RowDataBound event. Try the following code and if you are facing any deficulty post comments here.

Html Source:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RowID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Font-Size="Small" DataSource='<%#getvalues() %>' DataTextField="EventName"
DataValueField="EventID" Width="150px"                             onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField  HeaderText="Name">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Font-Size="Small" Width="150px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Code Behind:

   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {        
            e.Row.Cells[2].Text = System.DateTime.Now.Year.ToString();
        }
    }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.