Home > .net, AJAX, javascript, jQuery, WCF > Working with jqGrid and WCF service

Working with jqGrid and WCF service

Today , I am going to show how to work with jqGrid with WCF in asp.net application. jqGrid is an Ajax-enabled JavaScript control that provides solutions for representing and manipulating tabular data on the web.You can download a copy of the code from here

1. To start with , we need to download the latest version of jQuery and jqGrid plug-in. At the time of writing ,the latest version of jqGrid is jqGrid 3.5 beta. The major differences for this version is that starting with this version, jqGrid does not use a loader (which loads the needed files one by one), but all the needed code is contained in one file. The desired modules can be built using the jqGrid download manager . In order to use this, first a language file should be loaded and then the jqGrid file.

2. Next, you need to add JavaScript into your project and references using script tag. In order to use jqGrid 3.5, first a UI theme CSS file should be loaded. The detailed instruction is in the downloaded jqGrid zip file.Then,we need to create an AJAX-Enabled WCF Service in our asp.net web project. We need to create an method in the WCF service and add the following attributes to the method.

[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json
public string GetProducts()
IList personList = ProductRepository.GetProducts();
int total = 1, page = 10;
ProductJqGridView productJqGridView = new ProductJqGridView(total, page, personList);
return productJqGridView.ToJson();

GetProducts method will return a list of products.After that I am instantiate ProductJqGridView object and call ToJson method to serialize the object into the correct JOSN format so that the jqGrid can consume it.

3. Before we looked the JavaScript ,we need to see what required in the markup. We need a table to display the data ,a div for the page navigation and button to load the data.Please see the source code for details.

4. Last, we are going to look at how to write JavaScript to bind the JSON data to the jqGrid.We use function as our datatype.The function we are using is getProducts which doing an AJAX call to the server and a JSON string is returned.In the successFunction we insert all the data into the jqGrid by calling thegrid.addJSONData.

function successFunction(jsondata) {
var thegrid = jQuery("#productGridView")[0];

function getProducts() {
url: “linkification-ext” href=”http://DataLoader.svc/GetProducts” title=”Linkification: http://DataLoader.svc/GetProducts“>DataLoader.svc/GetProducts“,
data: “{}”, // For empty input data use “{}”,
dataType: “json”,
type: “POST”,
contentType: “application/json; charset=utf-8″,
success: successFunction
function dataBindToGrid() {
datatype: getProducts,
colNames: ['ProductID', 'Name', 'ProductNumber', 'Color', 'Size', 'Style'],
colModel: [{ name: 'ProductID', index: 'ProductID', width: 200, align: 'left' },
{ name: 'Name', index: 'Name', width: 200, align: 'left' },
{ name: 'ProductNumber', index: 'ProductNumber', width: 200, align: 'left' },
{ name: 'Color', index: 'Color', width: 200, align: 'left' },
{ name: 'Size', index: 'Size', width: 200, align: 'left' },
{ name: 'Style', index: 'Style', width: 200, align: 'left' }
rowNum: 10,
rowList: [5, 10, 20, 50, 100],
sortname: ‘ProductID’,
pager: jQuery(‘#pageNavigation’),
sortorder: “desc”,
viewrecords: true
jQuery(document).ready(function() {
jQuery(“input#LoadData”).live(“click”, dataBindToGrid);

A word of caution…

This is only a simple example using jqGrid, many of its functionalities have not been implemented. Since this is only a “Hello World” like example, I will show some of its advanced functionalities in some other article.
The hardest part for using AJAX and jqGrid is not the concept but the level of JavaScript skills. To be able to write maintainable AJAX code, you need to learn JavaScript. I have created an article on my blog to help you learn JavaScript. Here is the link. I hope my examples can help people get started using jqGrid using WCF in an ASP.NET web application. Your feedback is much more appreciated.

kick it on DotNetKicks.com

About these ads
Categories: .net, AJAX, javascript, jQuery, WCF
  1. abdul
    February 9, 2012 at 12:52 am | #1

    nice post kindly help me in paging and sorting

  1. October 2, 2011 at 3:05 pm | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 155 other followers

%d bloggers like this: