Articles in this section
Category / Section

How to update headerText for rendered grid?

1 min read

We can update the header text of a grid columns after rendering the grid by using external button. In the click event of Button, using columns method of grid we can add new column into grid or modified already existing column in the grid. The following code example shows how to update the headerText of the column in Grid.

  1. Render the Grid control.

JS

<input type="button" value="UpdateHeaderText " onclick="btnClick()" />
<div id="Grid"></div>
<script type="text/javascript">
    $(function () {// Document is ready.        
        $("#Grid").ejGrid({
            dataSource: window.gridData, 
            allowPaging: true,
              columns: [
                       { field: "OrderID", headerText: "Order ID",  width: 100 },
                      { field: "CustomerID", headerText: "Customer ID", width: 100 },
                      { field: "EmployeeID", headerText: "Employee ID", width: 100  }, 
                      { field: "Freight", headerText: "Freight", width: 100, format: "{0:C}" },                      
                      {field: "ShipCountry", headerText: "Ship Country", width: 100 }             
            ]
    });
</script>

MVC

[In View]
<input type="button" value="UpdateHeaderText" onclick="btnClick()" />
@(Html.EJ().Grid<object>("Grid")
        .Datasource((IEnumerable<object>)ViewBag.data)        
        .AllowPaging()
        .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").Width(100).Add();
            col.Field("CustomerID").HeaderText("Customer ID").Width(100).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").Width(100).Add();
            col.Field("Freight").HeaderText("Freight").Format("{0:C}”).Width(100).Add();
            col.Field("ShipCountry").HeaderText("Ship Country").Width(100).Add();
        })
   )
[Controller]
namespace EJGrid.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var DataSource = OrderRepository.GetAllRecords();
            ViewBag.data = DataSource;
            return View();
        }        
    }
}

ASP.NET

[aspx]
<input type="button" value="UpdateHeaderText " onclick="btnClick()" />
 <ej:Grid ID="Grid" runat="server" AllowPaging="True" >  
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID” Width="100"/>                
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="100"/>
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" Width="100"/>    
                <ej:Column Field="Freight" HeaderText="Freight" Format="{0:C}" Width="100"/>
                <ej:Column Field="ShipCountry" HeaderText="Ship Country" Width="100"/>                            
            </Columns>
</ej:Grid>  

2. In the button click event, we can get the corresponding column that we are going to change the header text by using getColumnByHeaderText method of Grid. Then the updated column object is passed to the columns method of the grid in order to refresh the grid header.

JS

    function btnClick(args) {
        var gridobj = $("#Grid").data("ejGrid");        
        var col = gridobj.getColumnByHeaderText("Freight");//get the JSON object of the column corresponding to the headerText
        col.headerText = "Changed Text";// assign a new header text to the column
        gridobj.columns(col);//pass the updated column object to the columns method
    }

 

The following output is displayed as a result of the above code example.

Figure: Grid column with updated header text

 

 

 

 

 

 

                           

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied