The following is my Data Table in Design Mode from which I will show data in a jqGrid:

table design
Image 1.

The following is the script of my Data Table:

  1. CREATE TABLE [dbo].[Employee](
  2. [Emp_ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Designation] [varchar](50) NULL,
  5. [City] [varchar](50) NULL,
  6. [State] [varchar](50) NULL,
  7. [Country] [varchar](50) NULL,
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  9. (
  10. [Emp_ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO

The data in My Table is:

table
Image 2.

The following is my aspx code:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="jQGridExample.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <link type="text/css" href="http://jqueryrock.googlecode.com/svn/trunk/css/jquery-ui-1.9.2.custom.css" rel="stylesheet" />
  7. <link type="text/css" href="http://jqueryrock.googlecode.com/svn/trunk/jqgrid/css/ui.jqgrid.css" rel="stylesheet" />
  8. <script type="text/javascript" src="http://jqueryrock.googlecode.com/svn/trunk/js/jquery-1.8.3.js"></script>
  9. <script type="text/javascript" src="http://jqueryrock.googlecode.com/svn/trunk/js/jquery-ui-1.9.2.custom.js"></script>
  10. <script src="http://jqueryrock.googlecode.com/svn/trunk/jqgrid/js/grid.locale-en.js" type="text/javascript"></script>
  11. <script src="http://jqueryrock.googlecode.com/svn/trunk/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
  12. <script type="text/javascript">
  13. $(function () {
  14. $("#dataGrid").jqGrid({
  15. url: 'Default.aspx/GetDataFromDB',
  16. datatype: 'json',
  17. mtype: 'POST',
  18. serializeGridData: function (postData) {
  19. return JSON.stringify(postData);
  20. },
  21. ajaxGridOptions: { contentType: "application/json" },
  22. loadonce: true,
  23. colNames: ['Employee ID', 'Name', 'Designation', 'City', 'State', 'Country'],
  24. colModel: [
  25. { name: 'Emp_ID', index: 'Employee ID', width: 80 },
  26. { name: 'Name', index: 'Name', width: 140 },
  27. { name: 'Designation', index: 'Designation', width: 160 },
  28. { name: 'City', index: 'City', width: 180 },
  29. { name: 'State', index: 'State', width: 180 },
  30. { name: 'Country', index: 'Country', width: 180 }
  31. ],
  32. pager: '#pagingGrid',
  33. rowNum: 5,
  34. rowList: [10, 20, 30],
  35. viewrecords: true,
  36. gridview: true,
  37. jsonReader: {
  38. page: function (obj) { return 1; },
  39. total: function (obj) { return 1; },
  40. records: function (obj) { return obj.d.length; },
  41. root: function (obj) { return obj.d; },
  42. repeatitems: false,
  43. id: "0"
  44. },
  45. caption: 'jQ Grid Example'
  46. });
  47. }).pagingGrid("#pager", { edit: true, add: true, del: false });
  48. </script>
  49. </head>
  50. <body style="font-family: Arial; font-size: 10pt">
  51. <table style="border: solid 15px red; width: 100%; vertical-align: central;">
  52. <tr>
  53. <td style="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; font-family: 'Times New Roman'; font-weight: bold; font-size: 20pt; color: chocolate;">jQ Grid Example In ASP.NET C#
  54. </td>
  55. </tr>
  56. <tr>
  57. <td style="text-align: center; vertical-align: central; padding: 50px;">
  58. <table id="dataGrid" style="text-align: center;"></table>
  59. <div id="pagingGrid"></div>
  60. </td>
  61. </tr>
  62. </table>
  63. </body>
  64. </html>

The following is my aspx.cs code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9. using System.Data;
  10. using System.Web.Script.Serialization;
  11. using System.Web.Services;
  12. namespace jQGridExample
  13. {
  14. public partial class Default : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. }
  19. [WebMethod]
  20. public static List<Dictionary<string, object>> GetDataFromDB()
  21. {
  22. DataTable dt = new DataTable();
  23. using (SqlConnection con = new SqlConnection(@"Data Source=INDIA\MSSQLServer2k8; Initial Catalog=EmployeeManagement; Uid=sa; pwd=india;"))
  24. {
  25. using (SqlCommand cmd = new SqlCommand("SELECT Emp_ID, Name, Designation, City, State,Country FROM Employee ORDER BY Emp_ID,Country,State, City", con))
  26. {
  27. con.Open();
  28. SqlDataAdapter da = new SqlDataAdapter(cmd);
  29. da.Fill(dt);
  30. System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
  31. List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
  32. Dictionary<string, object> row;
  33. foreach (DataRow dr in dt.Rows)
  34. {
  35. row = new Dictionary<string, object>();
  36. foreach (DataColumn col in dt.Columns)
  37. {
  38. row.Add(col.ColumnName, dr[col]);
  39. }
  40. rows.Add(row);
  41. }
  42. return rows;
  43. }
  44. }
  45. }
  46. }
  47. }

Now run the application.

All the records are in the jqGrid. Here I set the Page Size to 5.

jQGrid
Image 3.

Now do paging as in the following:

jQGrid example
Image 4.

paging
Image 5.

Now do paging
Image 6.

jQGrid image
Image 7.