JQGrid in DotNet Application
JQGrid in DotNet Application
- Load Once (Load the whole data at the time of page load and then make a grid out of it)
- Ajax Request based (Load the data in ajaxified fashion)
- Fetch data from database into dataset/any collection
- Serialize and store that data in a hidden field in JSON format like displayed in the personList shown below.
- Parse hidden field data at the client end (script end) and generate JQGrid.
var personList = [ {id:"1",name:"test1",note:"note23",amount:"200.00"}, {id:"2",name:"test2",note:"note2",amount:"300.00"}, {id:"3",name:"test3",note:"note32",amount:"400.00"}, {id:"4",name:"test23",note:"note423",amount:"700.00"}, {id:"5",name:"test2",note:"note2",amount:"300.00"}, {id:"6",name:"test333",note:"note344",amount:"500.00"} ]
<link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.8.13/themes/base/jquery-ui.css" /> <link type="text/css" rel="stylesheet" href="http://www.trirand.com/blog/jqgrid/themes/ui.jqgrid.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.13/jquery-ui.js"></script> <script type="text/javascript" src="https://www.trirand.com/blog/jqgrid/js/jquery.jqGrid.min.js"></script> <script type="text/javascript" src="https://www.trirand.com/blog/jqgrid/js/i18n/grid.locale-en.js"></script>
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web.Script.Serialization; private void PopulatePersons() { DataSet ds = new DataSet(); ArrayList list = new ArrayList(); ds = _person.LoadPersons(); foreach (DataRow dr in ds.Tables[0].Rows) { Hashtable entry = new Hashtable(); entry.Add("id", dr["personId"].ToString()); entry.Add("name", dr["name"].ToString()); entry.Add("note", dr["note"].ToString()); entry.Add("amount", dr["amount"].ToString()); list.Add(entry); } JavaScriptSerializer jserializer = new JavaScriptSerializer(); hdnPersonList.Value = jserializer.Serialize(list); }
<asp:HiddenField ID="hdnPersonList" runat="server" /> <table id="tblJQGrid"></table> <div id="divPager"></div>
<script language="javascript" type="text/javascript"> proceed = true; try { var valOfHidden = $.parseJSON(htmlDecode($("#<%= hdnPersonList.ClientID %>").val())); } catch (Exception) { proceed = false; // Do nothing } $(document).ready(function() { if (proceed) { createUserGrid(); } }); function createUserGrid() { $("#tblJQGrid").jqGrid({ data: valOfHidden, datatype: "local", height: 231, width: 937, colNames:['Number', 'Name', 'Amount', 'Notes'], colModel:[ {name:'id',index:'id', width:60, sorttype:"int"}, {name:'name',index:'name', width:100}, {name:'amount',index:'amount', width:80, align:"right",sorttype:"float"}, {name:'note',index:'note', width:150, sortable:false} ], //multiselect: true, rowNum:10, rowList:[10,20,30], loadonce: true, pager: '#divPager', caption: "Users Data" }); } function htmlDecode(value) { if (value) { return $('<div/>').html(value).text(); } } </script>