|
Index.cshtml
<button onclick="btnClick()">Render Grid</button>
<div class="control-section">
@(
@Html.EJS().Grid<gridmvclocalization.Controllers.OrdersDetails>("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("100").Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("100").Add();
col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();
}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).PageSettings(page => page.PageCount(2)).Render()
)
</div>
<script>
function btnClick() {
var grid = document.getElementById("Grid").ej2_instances[0];
grid.columns.push({ field: "ShipName", headerText: 'Ship Name', width: 120 }, { field: "ShipCity", headerText: 'Ship City', width: 120 }); //you can add the columns which was not defined in the columns list
grid.refreshColumns(); //After push the new column into Grid columns you have to call the refreshcolumns()
}
</script>
<script>
function btnClick () {
var grid = document.getElementById("Grid").ej2_instances[0];
grid.columns.push({ field: "ShipCity", headerText: 'Ship City', width: 150});
grid.refreshColumns();
}
</script>
|
Hi Abdul,
Greetings from syncfusion support
Based on your query we suspect that you need to add the columns dynamically. To achieve your requirement we suggest you to push the new column to the Grid columns property and then call the refreshColumns method of EJ2 Grid to refresh the changes in UI.
In below sample we have added news the columns using an external button. So we suggest you to follow the same way in your target function. Please refer the below code example for more information.
Index.cshtml
<button onclick="btnClick()">Render Grid</button><div class="control-section">@(@Html.EJS().Grid<gridmvclocalization.Controllers.OrdersDetails>("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col =>{col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width("100").Add();col.Field("CustomerID").HeaderText("Customer Name").Width("100").Add();col.Field("Freight").HeaderText("Freight").Width("160").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();}).AllowPaging().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).PageSettings(page => page.PageCount(2)).Render())</div><script>function btnClick() {var grid = document.getElementById("Grid").ej2_instances[0];grid.columns.push({ field: "ShipName", headerText: 'Ship Name', width: 120 }, { field: "ShipCity", headerText: 'Ship City', width: 120 }); //you can add the columns which was not defined in the columns listgrid.refreshColumns(); //After push the new column into Grid columns you have to call the refreshcolumns()}</script><script>function btnClick () {var grid = document.getElementById("Grid").ej2_instances[0];grid.columns.push({ field: "ShipCity", headerText: 'Ship City', width: 150});grid.refreshColumns();}</script>
Regards,Rajapandi R