Introduction
In this article, we will learn how to display data into Grid using JQWidget library. In this demo, we are going to create a database and a table called customer which contains data. We will use Web API 2 and Entity Framework ORM in order to create Service. Finally, for client side, we are using knockout.js library. I hope you will like this.
Let’s start.
Note
You can download zip file from JQWidget
Prerequisites
As I said before, we are going to use Web API 2 and Entity Framework. For this, you must have Visual Studio 2015 (.Net Framework 4.5.2) and SQL Server.
In this post, we are going to -
SQL Database part
Here, you find the scripts to create database and table.
Create Database
  1. USE [master]
  2. GO
  3. /****** Object: Database [DBCustomer] Script Date: 3/12/2017 4:38:33 AM ******/
  4. CREATE DATABASE [DBCustomer]
  5. CONTAINMENT = NONE
  6. ON PRIMARY
  7. ( NAME = N'DBCustomer', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBCustomer.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
  8. LOG ON
  9. ( NAME = N'DBCustomer_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBCustomer_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
  10. GO
  11. ALTER DATABASE [DBCustomer] SET COMPATIBILITY_LEVEL = 110
  12. GO
  13. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
  14. begin
  15. EXEC [DBCustomer].[dbo].[sp_fulltext_database] @action = 'enable'
  16. end
  17. GO
  18. ALTER DATABASE [DBCustomer] SET ANSI_NULL_DEFAULT OFF
  19. GO
  20. ALTER DATABASE [DBCustomer] SET ANSI_NULLS OFF
  21. GO
  22. ALTER DATABASE [DBCustomer] SET ANSI_PADDING OFF
  23. GO
  24. ALTER DATABASE [DBCustomer] SET ANSI_WARNINGS OFF
  25. GO
  26. ALTER DATABASE [DBCustomer] SET ARITHABORT OFF
  27. GO
  28. ALTER DATABASE [DBCustomer] SET AUTO_CLOSE OFF
  29. GO
  30. ALTER DATABASE [DBCustomer] SET AUTO_CREATE_STATISTICS ON
  31. GO
  32. ALTER DATABASE [DBCustomer] SET AUTO_SHRINK OFF
  33. GO
  34. ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS ON
  35. GO
  36. ALTER DATABASE [DBCustomer] SET CURSOR_CLOSE_ON_COMMIT OFF
  37. GO
  38. ALTER DATABASE [DBCustomer] SET CURSOR_DEFAULT GLOBAL
  39. GO
  40. ALTER DATABASE [DBCustomer] SET CONCAT_NULL_YIELDS_NULL OFF
  41. GO
  42. ALTER DATABASE [DBCustomer] SET NUMERIC_ROUNDABORT OFF
  43. GO
  44. ALTER DATABASE [DBCustomer] SET QUOTED_IDENTIFIER OFF
  45. GO
  46. ALTER DATABASE [DBCustomer] SET RECURSIVE_TRIGGERS OFF
  47. GO
  48. ALTER DATABASE [DBCustomer] SET DISABLE_BROKER
  49. GO
  50. ALTER DATABASE [DBCustomer] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
  51. GO
  52. ALTER DATABASE [DBCustomer] SET DATE_CORRELATION_OPTIMIZATION OFF
  53. GO
  54. ALTER DATABASE [DBCustomer] SET TRUSTWORTHY OFF
  55. GO
  56. ALTER DATABASE [DBCustomer] SET ALLOW_SNAPSHOT_ISOLATION OFF
  57. GO
  58. ALTER DATABASE [DBCustomer] SET PARAMETERIZATION SIMPLE
  59. GO
  60. ALTER DATABASE [DBCustomer] SET READ_COMMITTED_SNAPSHOT OFF
  61. GO
  62. ALTER DATABASE [DBCustomer] SET HONOR_BROKER_PRIORITY OFF
  63. GO
  64. ALTER DATABASE [DBCustomer] SET RECOVERY SIMPLE
  65. GO
  66. ALTER DATABASE [DBCustomer] SET MULTI_USER
  67. GO
  68. ALTER DATABASE [DBCustomer] SET PAGE_VERIFY CHECKSUM
  69. GO
  70. ALTER DATABASE [DBCustomer] SET DB_CHAINING OFF
  71. GO
  72. ALTER DATABASE [DBCustomer] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
  73. GO
  74. ALTER DATABASE [DBCustomer] SET TARGET_RECOVERY_TIME = 0 SECONDS
  75. GO
  76. ALTER DATABASE [DBCustomer] SET READ_WRITE
  77. GO
Create Table
  1. USE [DBCustomer]
  2. GO
  3. /****** Object: Table [dbo].[Customer] Script Date: 3/12/2017 4:38:53 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[Customer](
  11. [CustID] [int] IDENTITY(1,1) NOT NULL,
  12. [FirstName] [varchar](50) NULL,
  13. [LastName] [varchar](50) NULL,
  14. [Email] [varchar](50) NULL,
  15. [Country] [varchar](50) NULL,
  16. CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
  17. (
  18. [CustID] ASC
  19. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  20. ) ON [PRIMARY]
  21. GO
  22. SET ANSI_PADDING OFF
  23. GO
After creating the table, you can add some records as shown below.
create SQL Server table
Create your MVC application
Open Visual Studio and select File >> New Project.
The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.
Create MVC application
Next, new dialog will pop up for selecting the template. We are going choose Web API and click Ok.
create Web API
After creating our project, we are going to add ADO.NET Entity Data Model.
Adding ADO.NET Entity Data Model
In order to ADO.NET Entity Data Model, right click on the project name, click Add > Add New Item. Dialog box will pop up, inside Visual C# select Data then ADO.NET Entity Data Model, and enter name for your Dbcontext model as Model, finally click Add.
ADO.NET Entity Data Model
In this level, we are going to choose EF Designer from database as show below.
Entity Data Model Wizard
Next step, we need to choose a data connection which should be used to connect to the database. If the connection doesn’t exist, I will invite you to click on new connection button for creating the new one.
Entity Data Model Wizard
After clicking on next button, the dialog Entity Data Model Wizard will pop up for choosing the object which we want to use. In this demo we are going to choose customer table and click Finish button. Finally we see that EDMX model generates customer entity.
Entity Data Model Wizard
Create a Controller
Now, we are going to create a Controller. Right click on the Controllers folder > Add > Controller> selecting Web API 2 Controller – Empty > click Add.
Create MVC 5 Controller
Enter Controller name (‘CustomerController’).
Create MVC 5 Controller
CustomerController.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. namespace GridPaging.Controllers
  8. {
  9. public class CustomerController : ApiController
  10. {
  11. //Db Context
  12. private DBCustomerEntities db = new DBCustomerEntities();
  13. public IQueryable<Customer> GetCustomers()
  14. {
  15. return db.Customers;
  16. }
  17. }
  18. }
After creating our Controller, it’s time to implement GetCustomers() function which select all data from customer table.
Create HTML page
For doing this, right click on project name > Add > HTML Page.
Grid.html
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>.: Grid - KnockOut JS</title>
  5. <meta charset="utf-8" />
  6. <!-- CSS -->
  7. <link href="Content/jqx.base.css" rel="stylesheet" />
  8. </head>
  9. <body>
  10. <h3>Customer List</h3>
  11. <div>
  12. <div id="jqxgrid" data-bind="jqxGrid: { source: CustomerList, columns: [
  13. { text: 'Customer ID', dataField: 'CustID', width: 60 },
  14. { text: 'First Name', dataField: 'FirstName', width: 100},
  15. { text: 'Last Name', dataField: 'LastName', width: 100 },
  16. { text: 'Email', dataField: 'Email', width: 250 },
  17. { text: 'Country', dataField: 'Country', width: 100 }
  18. ], pageable: true, pagesize: 5, autoheight: true}">
  19. </div>
  20. </div>
  21. <!-- JS -->
  22. <script src="Scripts/jquery-1.10.2.min.js"></script>
  23. <script src="Scripts/knockout-3.4.0.js"></script>
  24. <!-- JQXWidget -->
  25. <script src="Scripts/jqxcore.js"></script>
  26. <script src="Scripts/jqxdata.js"></script>
  27. <script src="Scripts/jqxbuttons.js"></script>
  28. <script src="Scripts/jqxscrollbar.js"></script>
  29. <script src="Scripts/jqxmenu.js"></script>
  30. <script src="Scripts/jqxlistbox.js"></script>
  31. <script src="Scripts/jqxdropdownlist.js"></script>
  32. <script src="Scripts/jqxgrid.js"></script>
  33. <script src="Scripts/jqxgrid.selection.js"></script>
  34. <script src="Scripts/jqxgrid.edit.js"></script>
  35. <script src="Scripts/jqxgrid.pager.js"></script>
  36. <script src="Scripts/jqxknockout.js"></script>
  37. <script src="Scripts/demos.js"></script>
  38. <script type="text/javascript">
  39. $(document).ready(function () {
  40. var viewModel = function () {
  41. var self = this;
  42. self.CustID = ko.observable();
  43. self.FirstName = ko.observable();
  44. self.LastName = ko.observable();
  45. self.Email = ko.observable();
  46. self.Country = ko.observable();
  47. self.CustomerList = ko.observableArray([]);
  48. function getCustomers() {
  49. $.ajax({
  50. type: 'GET',
  51. url: '/api/Customer',
  52. contentType: 'application/json',
  53. success: function (data) {
  54. self.CustomerList(data);
  55. },
  56. error: function () {
  57. alert('Something Wrong !');
  58. }
  59. });
  60. }
  61. getCustomers();
  62. }
  63. ko.applyBindings(new viewModel());
  64. });
  65. </script>
  66. </body>
  67. </html>
Output
Now, build your application and you can see the following output.
Grid using JQWidget library