Change text of RadGrid Header Dynamically

Change text of RadGrid Header Dynamically

Step 1 :-Design web form .aspx page

<div>
            <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
            <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" AllowPaging="true" PageSize="8" Width="50%" PagerStyle-AlwaysVisible="true"
                GridLines="None" AutoGenerateColumns="false" OnPreRender="RadGrid1_PreRender" OnItemDataBound="RadGrid1_ItemDataBound1"
                OnNeedDataSource="RadGrid1_NeedDataSource">
                <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                <HeaderStyle HorizontalAlign="Left" />
                <MasterTableView DataKeyNames="name" AutoGenerateColumns="false">
                    <NoRecordsTemplate>No records found.</NoRecordsTemplate>

                    <Columns>
                        <telerik:GridBoundColumn HeaderText="Name" DataField="Name" />
                        <telerik:GridBoundColumn HeaderText="Amount" DataField="Amount" />
                        <telerik:GridBoundColumn HeaderText="Phone" DataField="Phone" />
                    </Columns>


                </MasterTableView>
            </telerik:RadGrid>
        </div>

Step 2 :-Logic on .CS page

Here i am using two method for changing Header Text Of Radgrid:

1)Change it via Pre_Render
2)Change it via Item_DataBound

Its On you what Method you wanna implements in your code according to need.


 

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Amount");
        dt.Columns.Add("Phone");


        dt.Rows.Add("Kandy", "12", "8802***2564");
        dt.Rows.Add("Sam", "25", "545502*2544");
        dt.Rows.Add("Izhar", "32", "95802*2452");
        dt.Rows.Add("Vineet", "10.2", "88022564***");


        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");
        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");
        dt.Rows.Add("Kandy", "0.12", "8802***2564");
        dt.Rows.Add("Sam", "12", "545502*2544");
        dt.Rows.Add("Izhar", "215", "95802*2452");
        dt.Rows.Add("Vineet", "11.00", "88022564***");

        RadGrid1.DataSource = dt;

    }

//We can change Header Text by many technique,so chnage it via Pre_Render Event
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        foreach (GridColumn col in RadGrid1.Columns)
        {
            if (col.UniqueName == "Name")
                col.HeaderText = "HeaderNameChangeViaPre_render";
            if (col.UniqueName == "Amount")
                col.HeaderText = "HeaderAmountChangeViaPre_render";
        }
        RadGrid1.Rebind();
    }

 

  //Change Header Text Using  ItemDataBound

    protected void RadGrid1_ItemDataBound1(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridHeaderItem)
        {
            GridHeaderItem header = (GridHeaderItem)e.Item;
            header["Name"].Text = "HeaderNameChangeViaItemDatBound";
            header["Amount"].Text = "HeaderAmountChangeViaItemDatBound";
        }
    }

You can Chose any method for changing the text .


The OutPut Will be Look like :-

Original Rad grid

Header Text Change Via Item_DataBound


Header Text Change Via Pre_Render


Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Telerik Radwindow Open on Button Click