English 中文(简体)
Linq2sql/GridView:分页不起作用?
原标题:
  • 时间:2009-03-11 14:03:37
  •  标签:

What s the best method to bind a linq2sql collection to a GridView, given that I want ot use builtin pagination and sorting?

我试过了。

public static void BindEnquiryList(EnquiryQuery query, GridView view)
        {
            DataContext db = DataContextManager.Context
            //view.DataSource = (from e in EnquiryMethods.BuildQuery(query)
            view.DataSource = (from e in db.Enquiries
                               select new
                               {
                                    Id = e.Id,
                                    Name = e.Name,
                                    PublicId = EnquiryMethods.GetPublicId(e.PublicId),
                                    What = e.WorkType.DescriptionText,
                                    Where = e.EnquiryArea.DescriptionText,
                                    Who = e.EnquiryType0.DescriptionText,
                                    When = e.EnquiryTime0.DescriptionText,
                                    PriceRange = e.EnquiryPrice0.DescriptionText,
                                    DisplayPriceRange = e.EnquiryPrice0.Display,
                                    Description = e.Description,
                                    Published = e.EnquiryPublished
                               });

            view.DataBind();
        }

GridView 设置为每页20条(并正确显示20条),但执行时间过长,当我记录SQL时得到:

SELECT [t0].[Id], [t0].[Name], [t0].[PublicId] AS [publicId], [t1].[DescriptionText] AS [What], [t2].[DescriptionText] AS [Where], [t3].[DescriptionText] AS [Who], [t4].[DescriptionText] AS [When], [t5].[DescriptionText] AS [PriceRange], [t5].[Display] AS [DisplayPriceRange], [t0].[Description], [t0].[EnquiryPublished] AS [Published]
FROM [dbo].[Enquirys] AS [t0]
LEFT OUTER JOIN [dbo].[WorkTypes] AS [t1] ON [t1].[Id] = [t0].[EnquiryPlace]
LEFT OUTER JOIN [dbo].[EnquiryAreas] AS [t2] ON [t2].[Id] = [t0].[EnquiryPlace]
LEFT OUTER JOIN [dbo].[EnquiryTypes] AS [t3] ON [t3].[Id] = [t0].[EnquiryType]
LEFT OUTER JOIN [dbo].[EnquiryTimes] AS [t4] ON [t4].[Id] = [t0].[EnquiryTime]
LEFT OUTER JOIN [dbo].[EnquiryPrices] AS [t5] ON [t5].[Id] = [t0].[EnquiryPrice]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8

没有任何顶部。我预计GridView来管理Take和Skip操作?

最佳回答

最好的方法是使用LinqDataSource。为LinqDataSource的选择事件创建事件处理程序。

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Result = (from e in db.Enquiries
                               select new
                               {
                                    Id = e.Id,
                                    Name = e.Name,
                                    PublicId = EnquiryMethods.GetPublicId(e.PublicId),
                                    What = e.WorkType.DescriptionText,
                                    Where = e.EnquiryArea.DescriptionText,
                                    Who = e.EnquiryType0.DescriptionText,
                                    When = e.EnquiryTime0.DescriptionText,
                                    PriceRange = e.EnquiryPrice0.DescriptionText,
                                    DisplayPriceRange = e.EnquiryPrice0.Display,
                                    Description = e.Description,
                                    Published = e.EnquiryPublished
                               });

}

然后LinqDataSource将为您处理分页和排序,并且您在Linq查询中得到编译检查。

问题回答

我不确定,但你能否使用LinqDataSource?否则,我想GridView需要数据在DataSet中才能自行进行分页和排序。

IEnumerable<T>不支持GridView控件的排序和分页。但您可以肯定使用List<T>。只需将结果集进行.ToList()处理。

grid_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        this.grid.PageIndex = e.NewPageIndex;
        this.grid.DataSource = this.DataSource;
        this.grid.DataBind();
    }




相关问题
热门标签