Your search did not match any results.
Data Grid

Web API Service

This example demonstrates the DataGrid widget communicating with a Web API service. Most of the DataGrid features are enabled, including the master-detail interface, header filter, summary, editing, and many more.

ASP.NET MVC

@(Html.DevExtreme().DataGrid() .Columns(columns => { columns.Add() .DataField("CustomerID") .Caption("Customer") .Lookup(lookup => lookup .DataSource(d => d.WebApi().Controller("DataGridWebApi").LoadAction("CustomersLookup").Key("Value")) .ValueExpr("Value") .DisplayExpr("Text") ); columns.Add() .DataField("OrderDate") .DataType(GridColumnDataType.Date); columns.Add() .DataField("Freight") .HeaderFilter(f => f.GroupInterval(100)); columns.Add() .DataField("ShipCountry"); columns.Add() .DataField("ShipRegion"); columns.Add() .DataField("ShipVia") .Caption("Shipping Company") .Lookup(lookup => lookup .DataSource(d => d.WebApi().Controller("DataGridWebApi").LoadAction("ShippersLookup").Key("Value")) .ValueExpr("Value") .DisplayExpr("Text") ); }) .DataSource(d => d.WebApi().Controller("DataGridWebApi").Key("OrderID")) .FilterRow(f => f.Visible(true)) .HeaderFilter(f => f.Visible(true)) .GroupPanel(p => p.Visible(true)) .Scrolling(s => s.Mode(GridScrollingMode.Virtual)) .Height(600) .ShowBorders(true) .MasterDetail(md => md .Enabled(true) .Template(@<text> @(Html.DevExtreme().DataGrid() .DataSource(d => d.WebApi() .Controller("DataGridWebApi") .LoadAction("OrderDetails") .LoadParams(new { orderID = new JS("data.OrderID") }) ) ) </text>) ) .Editing(e => e.AllowAdding(true).AllowDeleting(true).AllowUpdating(true)) .RemoteOperations(true) .Grouping(g => g.AutoExpandAll(false)) .Summary(s => s .TotalItems(totalItems => { totalItems.Add().Column("Freight").SummaryType(SummaryType.Sum); }) .GroupItems(groupItems => { groupItems.Add().Column("Freight").SummaryType(SummaryType.Sum); groupItems.Add().SummaryType(SummaryType.Count); }) ) )
using DevExtreme.MVC.Demos.Models; using DevExtreme.MVC.Demos.Models.SampleData; using System; using System.Linq; using System.Web.Mvc; namespace DevExtreme.MVC.Demos.Controllers { public class DataGridController : Controller { public ActionResult WebAPIService() { return View(); } } }
using DevExtreme.AspNet.Data; using DevExtreme.AspNet.Mvc; using Newtonsoft.Json; using System; using System.Linq; using System.Net; using System.Net.Http; using System.Net.Http.Formatting; using System.Web.Http; using DevExtreme.MVC.Demos.Models.Northwind; namespace DevExtreme.MVC.Demos.Controllers { public class DataGridWebApiController : ApiController { InMemoryNorthwindContext _nwind = new InMemoryNorthwindContext(); [HttpGet] public HttpResponseMessage Get(DataSourceLoadOptions loadOptions) { return Request.CreateResponse(DataSourceLoader.Load(_nwind.Orders, loadOptions)); } [HttpPost] public HttpResponseMessage Post(FormDataCollection form) { var values = form.Get("values"); var newOrder = new Order(); JsonConvert.PopulateObject(values, newOrder); Validate(newOrder); if(!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()); _nwind.Orders.Add(newOrder); _nwind.SaveChanges(); return Request.CreateResponse(HttpStatusCode.Created); } [HttpPut] public HttpResponseMessage Put(FormDataCollection form) { var key = Convert.ToInt32(form.Get("key")); var values = form.Get("values"); var order = _nwind.Orders.First(o => o.OrderID == key); JsonConvert.PopulateObject(values, order); Validate(order); if(!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.GetFullErrorMessage()); _nwind.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK); } [HttpDelete] public void Delete(FormDataCollection form) { var key = Convert.ToInt32(form.Get("key")); var order = _nwind.Orders.First(o => o.OrderID == key); _nwind.Orders.Remove(order); _nwind.SaveChanges(); } // additional actions [HttpGet] public HttpResponseMessage OrderDetails(int orderID, DataSourceLoadOptions loadOptions) { return Request.CreateResponse(DataSourceLoader.Load( from i in _nwind.Order_Details where i.OrderID == orderID select new { Product = i.Product.ProductName, Price = i.UnitPrice, Quantity = i.Quantity, Sum = i.UnitPrice * i.Quantity }, loadOptions )); } [HttpGet] public HttpResponseMessage ShippersLookup(DataSourceLoadOptions loadOptions) { var lookup = from i in _nwind.Shippers orderby i.CompanyName select new { Value = i.ShipperID, Text = i.CompanyName }; return Request.CreateResponse(DataSourceLoader.Load(lookup, loadOptions)); } [HttpGet] public HttpResponseMessage CustomersLookup(DataSourceLoadOptions loadOptions) { var lookup = from i in _nwind.Customers let text = i.CompanyName + " (" + i.Country + ")" orderby i.CompanyName select new { Value = i.CustomerID, Text = text }; return Request.CreateResponse(DataSourceLoader.Load(lookup, loadOptions)); } } }
using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace DevExtreme.MVC.Demos.Models.Northwind { public class InMemoryNorthwindContext { const string SessionKey = "b55756cf-fbdc-43b9-b091-1d2f15e1eb8e"; NorthwindContext _nwind = new NorthwindContext(); public DbSet<Customer> Customers { get { return _nwind.Customers; } } public DbSet<Order_Detail> Order_Details { get { return _nwind.Order_Details; } } public ICollection<Order> Orders { get { var session = HttpContext.Current.Session; if(session[SessionKey] == null) session[SessionKey] = _nwind.Orders.ToList(); return (ICollection<Order>)session[SessionKey]; } } public DbSet<Shipper> Shippers { get { return _nwind.Shippers; } } public void SaveChanges() { foreach(var order in Orders.Where(o => o.OrderID == 0)) { order.OrderID = Orders.Max(o => o.OrderID) + 1; } } } }