绑定 DataAdapter->DataSet->LINQ->ASP.Net DataGrid 时出错

发布于 2024-12-04 11:32:18 字数 2050 浏览 1 评论 0原文

我有以下代码:

using (SqlConnection cn = new SqlConnection(Connection.Instance.ConnectionString))
{
    // Open the connection
    using (SqlCommand cmd = new SqlCommand())
    {
        try
        {
            cmd.Connection = cn;

            cmd.CommandText = "Select Customers.CustomerID, Addresses.AddressCode, Addresses.FirstName, Addresses.LastName, Addresses.Address1, Addresses.City, Addresses.State, " +
            "Addresses.Zip, Addresses.Home AS HomePhone, Addresses.Phone AS WorkPhone, Addresses.EmailAddress  From Customers " +
            "LEFT OUTER JOIN Addresses ON Addresses.ID=Customers.AddressID " +
            "Where CustomerType IN ('HomeOwner', 'Home Owner') AND Customers.ResellerID=@ResellerID ";

            cmd.Parameters.AddWithValue("@ResellerID", base.UserID);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dsCustomer = new DataSet();
            da.Fill(dsCustomer);

            var customers = from c in dsCustomer.Tables[0].AsEnumerable().AsQueryable()
                            where c.Field<string>("CustomerID") == txtSearchCriteria.Text
                            select c;

            dgCustomers.CurrentPageIndex = 0;

            dgCustomers.DataSource = customers;
            dgCustomers.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message + e.StackTrace);
        }
        finally
        {
            if ((cn != null) && (cn.State != ConnectionState.Closed))
                cn.Close();
        }
    }
}

哪个给了我错误

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID 'dgCustomers' when AllowPaging is set to true and the selected data source does not implement ICollection.   at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)

如何转换此 LINQ 查询以便它可以分页?

注意:这是我正在尝试做的事情的简化版本。我知道在这个例子中我可以简单地修改 SQL 语句以包含“And CustomerID=@CustomerID”并完全绕过 LINQ。但是,从更大的角度来看,我不能这样做。

I have the following code:

using (SqlConnection cn = new SqlConnection(Connection.Instance.ConnectionString))
{
    // Open the connection
    using (SqlCommand cmd = new SqlCommand())
    {
        try
        {
            cmd.Connection = cn;

            cmd.CommandText = "Select Customers.CustomerID, Addresses.AddressCode, Addresses.FirstName, Addresses.LastName, Addresses.Address1, Addresses.City, Addresses.State, " +
            "Addresses.Zip, Addresses.Home AS HomePhone, Addresses.Phone AS WorkPhone, Addresses.EmailAddress  From Customers " +
            "LEFT OUTER JOIN Addresses ON Addresses.ID=Customers.AddressID " +
            "Where CustomerType IN ('HomeOwner', 'Home Owner') AND Customers.ResellerID=@ResellerID ";

            cmd.Parameters.AddWithValue("@ResellerID", base.UserID);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dsCustomer = new DataSet();
            da.Fill(dsCustomer);

            var customers = from c in dsCustomer.Tables[0].AsEnumerable().AsQueryable()
                            where c.Field<string>("CustomerID") == txtSearchCriteria.Text
                            select c;

            dgCustomers.CurrentPageIndex = 0;

            dgCustomers.DataSource = customers;
            dgCustomers.DataBind();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message + e.StackTrace);
        }
        finally
        {
            if ((cn != null) && (cn.State != ConnectionState.Closed))
                cn.Close();
        }
    }
}

Which is giving me the error

AllowCustomPaging must be true and VirtualItemCount must be set for a DataGrid with ID 'dgCustomers' when AllowPaging is set to true and the selected data source does not implement ICollection.   at System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)

How do I convert this LINQ query so that it can be pagable?

Note: This is a simplified version of what I'm trying to do. I know in this example I could simply modify the SQL statement to include "And CustomerID=@CustomerID" and bypass LINQ completely. But, in the bigger picture, I can't do that.

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

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

发布评论

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

评论(1

尬尬 2024-12-11 11:32:18

错误消息很清楚,您需要实现分页逻辑才能利用分页。顺便说一句,要使您的代码正常工作,只需使用 ICollection 作为数据源,更改此行:

dgCustomers.DataSource = customers.ToList();

The error message is clear, you need to implement your paging logic to take advantage from paging. BTW, to make your code to work just use a ICollection as DataSource, changing this line:

dgCustomers.DataSource = customers.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文