John Sourcer
19 Jun 2007, 11:39 PM
Hi All,
Quick tutorial to help C#'s prepare data for paging grid.
Firstly your SQL statement. In order to handle paging correctly you need to return a row count int for each row in order to set your 'start' and 'limit' parameters AND a total number of records. The following SQL statement does this easily using derived tables and can be adapted for larger table sets and UNIONS:
SELECT RowID, cnt, col1, col2, col3 FROM (
SELECT Row_Number() OVER (ORDER BY col1) AS RowID, count(*) over() as cnt , col1, col2, col3
FROM ( SELECT col1, col2, col3 FROM table) t
) d WHERE RowID >= @start AND RowID <= @limit
GROUP BY RowID, cnt, col1, col2, col3
The above statement gives you 'RowID' for each row and a column 'cnt' which counts the total records in the query EVEN though you are only returning a set amount. The downside is that there is an extra column but that far outweighs having to make an extra call to teh database.
Now we have our SQL prepared, let's call and convert this into JSON via C#:
//Include the nessecary using statements.
//Bare in mind that web controls etc are not needed as we are simply returning text
//String var to hold the total records count
string t = string.Empty;
//Increment you page limit by your start value
limit = limit + start;
DataSet ds = GetDS(start, limit);//Populate your dataset here calling the SQL above
//Convert your dataset mapping type to attribute based
foreach (DataColumn dc in ds.Tables[0].Columns)
dc.ColumnMapping = MappingType.Attribute;
//Get the total record count and drop the extraneous column 'cnt'
foreach (DataRow dr in ds.Tables[0].Rows)
t = Convert.ToString(dr[1]);
ds.Tables[0].Columns.RemoveAt(1);
// 'Convert' the dataset into an XmlDataDocument
XmlDataDocument oXml = new XmlDataDocument(ds);
//Format the XmlDataDocument into JSON. I am using Newtonsoft here
//but you could do this anyway you would like.
//Notice that the 't' string holding total record count has been concat'd into the JSON
string text = Newtonsoft.Json.JavaScriptConvert.SerializeXmlNode(oXml);
text = text.Replace("{NewDataSet:[{Table:", "{\"totalCount\": " + t + ", \"data\":");
text = text.Replace(",@", ",");
text = text.Replace("@RowID", "RowID");
text = text.Substring(0, text.Length - 2);
//Voila! Return the text
Response.Write(text);
Now the rest is easy:
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'ReturnYourJSON.aspx'}),
reader: new Ext.data.JsonReader({
root: 'data'
,totalProperty: 'totalCount'
,id: 'col1'
}, ['col2', 'col3'])
,remoteSort: true
});
var cm = new Ext.grid.ColumnModel([
{header: "Col1", width: 250, dataIndex: 'col1'},
{header: "Col2", width: 50, dataIndex: 'col2'},
{header: "Col3", width: 95, dataIndex: 'col3'}
]);
cm.defaultSortable = true;
var grid = new Ext.grid.Grid('mygrid', {
ds: ds
,cm: cm
,autoHeight: true
,autoWidth: true
,fitToFrame: true
});
var gPanel = new Ext.GridPanel(grid,{fitToFrame: true});
layout.add('center', gPanel);
grid.render();
var gridFoot = grid.getView().getFooterPanel(true);
var paging = new Ext.PagingToolbar(gridFoot, ds, {
pageSize: 25,
displayInfo: true,
displayMsg: 'Displaying products {0} - {1} of {2}',
emptyMsg: "No products to display"
});
ds.load({params:{start: 0, limit: 25}});
Hope this helps someone out there! Shout with any comments, suggestions.
Quick tutorial to help C#'s prepare data for paging grid.
Firstly your SQL statement. In order to handle paging correctly you need to return a row count int for each row in order to set your 'start' and 'limit' parameters AND a total number of records. The following SQL statement does this easily using derived tables and can be adapted for larger table sets and UNIONS:
SELECT RowID, cnt, col1, col2, col3 FROM (
SELECT Row_Number() OVER (ORDER BY col1) AS RowID, count(*) over() as cnt , col1, col2, col3
FROM ( SELECT col1, col2, col3 FROM table) t
) d WHERE RowID >= @start AND RowID <= @limit
GROUP BY RowID, cnt, col1, col2, col3
The above statement gives you 'RowID' for each row and a column 'cnt' which counts the total records in the query EVEN though you are only returning a set amount. The downside is that there is an extra column but that far outweighs having to make an extra call to teh database.
Now we have our SQL prepared, let's call and convert this into JSON via C#:
//Include the nessecary using statements.
//Bare in mind that web controls etc are not needed as we are simply returning text
//String var to hold the total records count
string t = string.Empty;
//Increment you page limit by your start value
limit = limit + start;
DataSet ds = GetDS(start, limit);//Populate your dataset here calling the SQL above
//Convert your dataset mapping type to attribute based
foreach (DataColumn dc in ds.Tables[0].Columns)
dc.ColumnMapping = MappingType.Attribute;
//Get the total record count and drop the extraneous column 'cnt'
foreach (DataRow dr in ds.Tables[0].Rows)
t = Convert.ToString(dr[1]);
ds.Tables[0].Columns.RemoveAt(1);
// 'Convert' the dataset into an XmlDataDocument
XmlDataDocument oXml = new XmlDataDocument(ds);
//Format the XmlDataDocument into JSON. I am using Newtonsoft here
//but you could do this anyway you would like.
//Notice that the 't' string holding total record count has been concat'd into the JSON
string text = Newtonsoft.Json.JavaScriptConvert.SerializeXmlNode(oXml);
text = text.Replace("{NewDataSet:[{Table:", "{\"totalCount\": " + t + ", \"data\":");
text = text.Replace(",@", ",");
text = text.Replace("@RowID", "RowID");
text = text.Substring(0, text.Length - 2);
//Voila! Return the text
Response.Write(text);
Now the rest is easy:
var ds = new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'ReturnYourJSON.aspx'}),
reader: new Ext.data.JsonReader({
root: 'data'
,totalProperty: 'totalCount'
,id: 'col1'
}, ['col2', 'col3'])
,remoteSort: true
});
var cm = new Ext.grid.ColumnModel([
{header: "Col1", width: 250, dataIndex: 'col1'},
{header: "Col2", width: 50, dataIndex: 'col2'},
{header: "Col3", width: 95, dataIndex: 'col3'}
]);
cm.defaultSortable = true;
var grid = new Ext.grid.Grid('mygrid', {
ds: ds
,cm: cm
,autoHeight: true
,autoWidth: true
,fitToFrame: true
});
var gPanel = new Ext.GridPanel(grid,{fitToFrame: true});
layout.add('center', gPanel);
grid.render();
var gridFoot = grid.getView().getFooterPanel(true);
var paging = new Ext.PagingToolbar(gridFoot, ds, {
pageSize: 25,
displayInfo: true,
displayMsg: 'Displaying products {0} - {1} of {2}',
emptyMsg: "No products to display"
});
ds.load({params:{start: 0, limit: 25}});
Hope this helps someone out there! Shout with any comments, suggestions.