- Home
- Forum
- ASP.NET MVC
- Add columns dynamically from controller or through script
Add columns dynamically from controller or through script
Thanks for using Syncfusion Products.
We can update the Grid columns using either at the client side or else using the code behind.
Using Columns() of ejGrid, we can dynamically update/remove the columns from the Grid and also we can push column object to the column of Grid model. Refer to the following code example and Help Document.
|
@(Html.EJ().Button("ClickMe").Text("Click Me").Type(ButtonType.Button).ClientSideEvents(events=>events.Click("click")))
@(Html.EJ().Grid<object>("FlatGrid") .Datasource((IEnumerable<object>)ViewBag.dataSource) .. . . .ClientSideEvents(events=>events.Load("load")) )
<script> function load(args) { this.model.columns.push({ field: "ShipName", headerText: "Ship Name" }); this.model.columns.push({ field: "EmployeeID", headerText: "Employee ID" }); } function click(args) {//update grid columns using columns() var obj = $("#FlatGrid").ejGrid("instance"); obj.columns("CustomerID", "add");//Add column obj.columns("CustomerID", "remove");//remove column obj.columns([{ field: "ShipCity", headerText: "ShipCity" }])//Add an array of object } |
http://help.syncfusion.com/js/api/ejgrid#methods:columns
Using GridProperties, we can update the Grid columns from controller end. Refer to the code example.
|
@(Html.EJ().Grid<object>("FlatGrid") .Datasource((IEnumerable<object>)ViewBag.dataSource) .. . .. .Columns(ViewBag.cols) //use the columns ViewBag namespace MvcApplication66.Controllers { public class HomeController : Controller { public ActionResult Index() { ViewBag.dataSource = OrderRepository.GetAllRecords(); List<Column> cols = new List<Column>();// Column is a class of Syncfusion Javascript models cols.Add(new Column() { Field = "OrderID", HeaderText = "Order ID"});//Add some column ViewBag.cols = cols; //update them in ViewBag return View(); } } |
We have prepared a sample that can be downloaded from the following location.
Sample: http://www.syncfusion.com/downloads/support/forum/123170/ze/Dynamic_Columns-1104901244
Regards,
Seeni Sakthi Kumar S.
- 1 Reply
- 2 Participants
-
GO Gomtesh
- Feb 23, 2016 11:03 AM UTC
- Feb 24, 2016 06:22 AM UTC