A Simple JqGrid Demo Using WCF

jqGrid provides a way for representing and manipulating tabular data on the web using AJAX. jqGrid can be used with any server-side technology, including ColdFusion, Perl, PHP, ASP, Java Servlets and JSP, since it is a client-side solution loading data dynamically using Ajax callbacks.

jqGrid's home page can be found here.

In this article we will see how to work with jqGrid and WCF in an ASP.NET application. Here we will bind a JSON object to a jqGrid using WCF.

Working with jqGrid mainly consists of the following three steps.

1. Adding references

First, we need to download jqGrid from here. Then add the references to the page as shown below.

Adding references

2. Placement in the HTML

We need to place an empty <table> tag with a unique id in our HTML where we want the jqGrid to appear. The paging is optional. But if we need pagination we need to add a <div> tag too with a unique id along with the <table> tag. As shown below.

Placement in the HTML

Here the table tag and div tag for pagination has been used.

3. Grid setting

This is the most important step, here we define the grid, the number of columns, type of columns and so on.

Grid setting

Then, we need to create an AJAX-enabled WCF Service in our ASP.NET web project. We need to create a method and add the following attributes to the method.

attributes to the method

create an AJAX

And we also need to define an ajax call in our script file. The following code shows how to.

  1. $(document).ready(function () {  
  2.    $("#divMyGrid").jqGrid({  
  3.       ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },  
  4.       prmNames: {  
  5.          rows: "numRows",  
  6.          page: "pageNumber"  
  7.       },  
  8.       colNames: ['Id''File Name''Uploaded By''Uploaded On''Size''Status'], //define column names  
  9.       colModel: [  
  10.          { name: 'id', index: 'id', key: true, width: 100 },  
  11.          { name: 'fileName', index: 'fileName', width: 400 },  
  12.          { name: 'uploadedBy', index: 'uploadedBy', width: 200 },  
  13.          { name: 'uploadedOn', index: 'uploadedOn', width: 200 },  
  14.          { name: 'size', index: 'size', width: 50 },  
  15.          { name: 'status', index: 'status', width: 50}  
  16.       ], //define column models  
  17.       datatype: function (postdata) {  
  18.          var dataUrl = 'IgroupUploaderService.svc/GetAllFiles'  
  19.          $.ajax({  
  20.             url: dataUrl,  
  21.             type: "POST",  
  22.             contentType: "application/json; charset=utf-8",  
  23.             dataType: "json",  
  24.             data: JSON.stringify(postdata),  
  25.             success: function (data, st) {  
  26.                if (st == "success" && JSON.parse(data.d.indexOf("_Error_") != 0)) {  
  27.                   var grid = $("#divMyGrid")[0];  
  28.                   grid.addJSONData(JSON.parse(data.d));  
  29.                }  
  30.             },  
  31.             error: function () {  
  32.                alert("Error while processing your request");  
  33.             }  
  34.          });  
  35.       },  
  36.       pager: '#divPaging',  
  37.       sortname: 'id'//the column according to which data is to be sorted(optional)  
  38.       viewrecords: true//if true, displays the total number of records, etc. as: "View X to Y out of Z” (optional)  
  39.       sortorder: "asc"//sort order(optional)  
  40.       caption: "Files"//Sets the caption for the grid. If this parameter is not set the Caption layer will be not visible  
  41.       multiselect: true,  
  42.       rowNum: 20,  
  43.       loadonce: false,  
  44.       autowidth: true,  
  45.       shrinkToFit: true,  
  46.       height: '100%',  
  47.       rowList: [10, 20, 30, 50, 100],  
  48.       sortable: true  
  49.    }).navGrid("#divPaging", { search: true, edit: false, add: false, del: false }, {}, {}, {}, { multipleSearch: false });  
  50. });   
In the success function, we bind all the data to the jqGrid by calling grid.addJSONData.

jqGrid

jqGrid provides multiple features, such as inline editing, searching, filtering and sorting and so on. But in this article we only discussed the binding of a JSON object to a jqGrid using WCF services in an ASP.NET web application. I hope it helps you all in getting started with jqGrid.

Up Next
    Ebook Download
    View all
    Learn
    View all