views:

91

answers:

1

I want to bind a Telerik RadGrid to a web service without using Linq. In all examples I can find, the web service has to return a List(Of MyObject); I've tried this, and it works great. However, the table I'm binding to may at runtime have additional columns, or columns may have different data type, so I can't use a static MyObject class to represent the table at compile time. I also don't know at compile time which columns need to be displayed in the grid. I would like to bind to a web service for performance reasons.

I've tried having the web service method return a DataView, and cast it a lot of different ways, but it's not working. How would I write the GetData / GetDataAndCount method of the web service to return the data from a DataView or other non-linq data source?

Thanks.

A: 

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;
}

}

Vlad
Thanks very much, that worked great!
Alfred