[]
The quick start guides you through the steps of adding FlexGrid control in your MVC web application and adding data to it using CollectionView class. Complete the following steps to see how the FlexGrid control appears after data binding:
Create a new MVC application using the ComponentOne or VisualStudio templates. For more information about creating an MVC application, see Configuring your MVC Application topic.
type=note
The example uses C1NWind database. The C1NWind.mdf file is available on your system at the following location:
Documents\ComponentOne Samples\ASP.NET MVC\MVC\MvcExplorer\App_Data
AppData
folder in the Solution Explorer.Models|Add New Item|Data
, and select ADO.NET Entity Data Model.Tables
, and click Finish.If you can see C1NWind.edmx added to your project under the Models folder, you have successfully configured the data source for your application.
Complete the following steps to initialize a FlexGrid control.
Add a new Controller
In the Solution Explorer, right click the folder Controllers.
From the context menu, select Add | Controller. The Add Scaffold dialog appears.
Complete the following steps in the Add Scaffold dialog:
Default1Controller
).Include the MVC references as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollectionView_EN.Models;
using C1.Web.Mvc.Grid;
using C1.Web.Mvc;
using C1.Web.Mvc.Serializition;
using System.Data.Entity.Validation;
using System.Data.Entity;
using System.Data;
Replace the method Index() with the following method.
IndexController.cs
//Define datasource for FlexGrid
private C1NWindEntities db = new C1NWindEntities();
public ActionResult Index()
{
return View();
}
//Instantiate a JSON request
public ActionResult GridReadCategory([C1JsonRequest] CollectionViewRequest<Category> requestData)
{
return this.C1Json(CollectionViewHelper.Read(requestData, db.Categories));
}
Add a View for the controller:
From the Solution Explorer, expand the folder Controllers and double click the controller (for example: Default1Controller
) to open it.
Place the cursor inside the method Index()
.
Right click and select Add View. The Add View dialog appears.
In the Add View dialog, verify that the View name is Index and View engine is Razor (CSHTML).
Click Add. A view has been added for the controller.
In the Solution Explorer, double click Index.cshtml
to open it.2. Replace the default code of the Views\Index.cshtml file with the one given below to initialize a FlexGrid control.
In this example, the GridReadCategory action of controller is assigned to Bind property of FlexGrid' ItemSource to populate data.
<c1-flex-grid id="fGRCView" auto-generate-columns="false" allow-add-new="true"
allow-sorting="true" class="grid">
<c1-items-source read-action-url="@Url.Action("GridReadCategory")"></c1-items-source>
<c1-flex-grid-column binding="CategoryID"></c1-flex-grid-column>
<c1-flex-grid-column binding="CompanyName"></c1-flex-grid-column>
<c1-flex-grid-column binding="Description"></c1-flex-grid-column>
</c1-flex-grid>