如何在jqxgrid中使用后面代码中的数据表

嗨我有DataTable绑定到Gridview。 但需要将知道绑定到jqxgrid或jqxdatatable。 谷歌搜索了这么多次后,我没有得到适当的解决方案。

DataTable tb1= qry.GetTicketDetails(); serviceWindow.DataSource = tb; serviceWindow.DataBind(); 

这就是我现在正在做的事情。

在jquery中,我可以使用下面显示的XML。

  var source = { dataType: "json", dataFields: [ { name: 'name', type: 'string' }, { name: 'type', type: 'string' }, { name: 'calories', type: 'int' }, { name: 'totalfat', type: 'string' }, { name: 'protein', type: 'string' } ], id: 'id', url: "data/ticket.XML", //how to take datatable from code behind file }; 

.aspx代码 – >>

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestCellSelection.aspx.cs" Inherits="CourierApp.Project.TestCellSelection" %>                            
<%----%>

.aspx.cs或Web服务代码—– >>

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Services; using System.Data; using System.Data.SqlClient; using System.Collections; using System.Web.Script.Serialization; using System.Web.Helpers; using BL; using DAL; using Newtonsoft.Json; public partial class Project_Service : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } #region Testing [WebMethod] public static String GridValues() { String Qry = "SELECT [From Kgs] as From_Kg,[To kgs] as To_Kg,[1] as Zone1,[2] as Zone2,[3] as Zone3,[4] as Zone4,[5] as Zone5,[6] as Zone6,[7] as Zone7,[8] as Zone8,[9] as Zone9 FROM Tnt_Rate"; DataTable dt = DbAccess.FetchDatatable(Qry); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List> rows = new List>(); Dictionary row; foreach (DataRow dr in dt.Rows) { row = new Dictionary(); foreach (DataColumn col in dt.Columns) { row.Add(col.ColumnName, dr[col]); } rows.Add(row); } String Val = serializer.Serialize(rows); if (Val != "") { return (Val); } else { return "Error"; } } [WebMethod] public static String Col_Datafeilds() { String Qry = "SELECT [From Kgs] as From_Kg,[To kgs] as To_Kg,[1] as Zone1,[2] as Zone2,[3] as Zone3,[4] as Zone4,[5] as Zone5,[6] as Zone6,[7] as Zone7,[8] as Zone8,[9] as Zone9 FROM Tnt_Rate"; DataTable dt = DbAccess.FetchDatatable(Qry); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List> rows = new List>(); Dictionary row = new Dictionary(); String Col = ""; int counter = 0; foreach (DataColumn col in dt.Columns) { if (counter < 2) { row = new Dictionary(); row.Add("name", col.ColumnName); row.Add("type", "string"); rows.Add(row); } else if (counter >= 2) { row = new Dictionary(); row.Add("name", col.ColumnName); row.Add("type", "number"); rows.Add(row); } counter += 1; } Col = serializer.Serialize(rows); //Col = JsonConvert.SerializeObject(rows); if (Col != "") { return (Col); } else { return "Error"; } } [WebMethod] public static String Col_Columns() { String Qry = "SELECT [From Kgs] as From_Kg,[To kgs] as To_Kg,[1] as Zone1,[2] as Zone2,[3] as Zone3,[4] as Zone4,[5] as Zone5,[6] as Zone6,[7] as Zone7,[8] as Zone8,[9] as Zone9 FROM Tnt_Rate"; DataTable dt = DbAccess.FetchDatatable(Qry); System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List> rows = new List>(); Dictionary row; String Col = ""; int counter = 0; foreach (DataColumn col in dt.Columns) { if (counter < 2) { row = new Dictionary(); row.Add("text", col.ColumnName.ToUpper()); row.Add("datafield", col.ColumnName); row.Add("columntype", "textbox"); row.Add("width", "100"); row.Add("cellsalign", "left"); //row.Add("pinned", "true"); rows.Add(row); } else if (counter >= 2) { row = new Dictionary(); row.Add("text", col.ColumnName.ToUpper()); row.Add("datafield", col.ColumnName); row.Add("columntype", "textbox"); row.Add("width", "25"); row.Add("cellsalign", "right"); rows.Add(row); } } Col = serializer.Serialize(rows); // Col = JsonConvert.SerializeObject(rows); if (Col != "") { return (Col); } else { return "Error"; } } #endregion } 

粘贴……

用您的查询替换查询…..

Donot Supply Datafeild和Column Def。 在.aspx页面中

这是完全动态的解决方案!