0

Linq を使用せずに、Telerik RadGrid を Web サービスにバインドしたいと考えています。私が見つけることができるすべての例で、Web サービスは List(Of MyObject); を返す必要があります。私はこれを試しましたが、うまくいきます。ただし、バインドしているテーブルには、実行時に追加の列が含まれているか、列のデータ型が異なる可能性があるため、静的な MyObject クラスを使用してコンパイル時にテーブルを表すことはできません。また、コンパイル時にグリッドに表示する必要がある列もわかりません。パフォーマンス上の理由から、Web サービスにバインドしたいと考えています。

Web サービス メソッドが DataView を返すようにして、さまざまな方法でキャストしようとしましたが、うまくいきません。Web サービスの GetData / GetDataAndCount メソッドを記述して、DataView またはその他の非 linq データ ソースからデータを返す方法を教えてください。

ありがとう。

4

1 に答える 1

0

私のブログ投稿と同様に DataTable をシリアル化できます。次に例を示します。

    <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;
    }
}
于 2010-09-27T08:40:05.793 に答える