You can serialize DataTable similar to my blog post. Here is an example:
<telerik:RadGrid ID="RadGrid1" AllowPaging="true" runat="server">
<MasterTableView>
<Columns>
<telerik:GridBoundColumn DataField="CustomerID" HeaderText="ID" />
<telerik:GridBoundColumn DataField="CompanyName" HeaderText="Name" />
<telerik:GridBoundColumn DataField="ContactName" HeaderText="Contact" />
<telerik:GridBoundColumn DataField="Country" HeaderText="Country" />
<telerik:GridBoundColumn DataField="City" HeaderText="City" />
</Columns>
</MasterTableView>
<PagerStyle AlwaysVisible="true" />
<ClientSettings>
<DataBinding Location="WebService.asmx" SelectMethod="GetDataAndCount" />
</ClientSettings>
</telerik:RadGrid>
<%@ WebService Language="C#" Class="WebService" %>
using System.Data;
using System.Linq;
using System.Web.Services;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public Telerik.Web.UI.GridBindingData GetDataAndCount(int startRowIndex, int maximumRows)
{
var table = GetDataTable("select * from customers");
var columns = table.Columns.Cast<System.Data.DataColumn>();
var data = table.AsEnumerable()
.Select(r => columns.Select(c => new { Column = c.ColumnName, Value = r[c] })
.ToDictionary(i => i.Column, i => i.Value != System.DBNull.Value ? i.Value : null))
.Skip(startRowIndex).Take(maximumRows)
.ToList<object>();
return new Telerik.Web.UI.GridBindingData() { Data = data, Count = table.Rows.Count };
}
public System.Data.DataTable GetDataTable(string query)
{
var connString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
var conn = new System.Data.SqlClient.SqlConnection(connString);
var adapter = new System.Data.SqlClient.SqlDataAdapter();
adapter.SelectCommand = new System.Data.SqlClient.SqlCommand(query, conn);
var table = new System.Data.DataTable();
conn.Open();
try
{
adapter.Fill(table);
}
finally
{
conn.Close();
}
return table;
}
}