Telerik RadGrid 无需 Linq 即可绑定到 Web 服务

发布于 2024-09-24 12:02:02 字数 356 浏览 0 评论 0原文

我想在不使用 Linq 的情况下将 Telerik RadGrid 绑定到 Web 服务。在我能找到的所有示例中,Web 服务必须返回一个 List(Of MyObject);我已经尝试过这个,效果很好。但是,我绑定到的表可能在运行时具有其他列,或者列可能具有不同的数据类型,因此我无法在编译时使用静态 MyObject 类来表示表。我也不知道在编译时需要在网格中显示哪些列。出于性能原因,我想绑定到 Web 服务。

我尝试过让 Web 服务方法返回 DataView,并以多种不同的方式对其进行转换,但它不起作用。如何编写 Web 服务的 GetData / GetDataAndCount 方法以从 DataView 或其他非 linq 数据源返回数据?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-10-01 12:02:02

您可以序列化 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文