首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RadGrid覆盖EntityDataSource排序

RadGrid覆盖EntityDataSource排序
EN

Stack Overflow用户
提问于 2012-12-20 22:12:55
回答 1查看 725关注 0票数 1

事前:

我正在使用一个静态数据库,它不能改变,因为它被外部软件使用。

如果数据库值不是"1“或"6”,我会将数据库值从StatusTypeId映射到介于这些值之间的值。背景: Supportticketsystem

客户应该只看到StatusTypeIds 1、3和6.1。1(打开)、3(工作中)、6(关闭)

因此,我手动更改了网格值,现在正在进行正确的排序。

在使用网格的排序函数之前,在EntityDataSource上执行的初始排序表达式工作正常。然后,无论我是否手动截取,排序都是不正确的。

EntityDataSource的初始排序:

代码语言:javascript
复制
<asp:EntityDataSource ID="DefectsDataSource" runat="server" 
ConnectionString="name=*" DefaultContainerName="*" 
EnableFlattening="False" EntitySetName="Defects" Select="it.[DefectId], it.[Name], it.[StatusTypeId]"  
OrderBy="(CASE WHEN it.[StatusTypeId] <> 1 AND it.[StatusTypeId] <> 6 THEN 2 ELSE it.[StatusTypeId] END)"/>

是否可以从网格(SortCommand事件)中获得请求的排序顺序,并设置EntityDataSource的OrderBy属性以正确排序?我试过了,但它被忽略了。

SortCommand事件的实现:

代码语言:javascript
复制
    protected void SortCommand(object sender, GridSortCommandEventArgs e)
    {
        if (e.SortExpression == "StatusTypeId")
        {
            string sortExpression = (e.NewSortOrder == GridSortOrder.Ascending) ? "ASC" : e.NewSortOrder == GridSortOrder.Descending ? "DESC" : "";
            this.DefectsDataSource.OrderBy = string.Format("(CASE WHEN it.[StatusTypeId] <> 1 AND it.[StatusTypeId] <> 6 THEN 2 ELSE it.[StatusTypeId] END) {0}", sortExpression);
            this.RadGrid.Rebind();
        }
    }

我希望有足够的信息来帮助我。如果我错过了什么,请问我。

EN

回答 1

Stack Overflow用户

发布于 2014-06-11 15:42:16

我已经解决了这个问题,通过使用ObjectContext.Entity.ToList().OrderBy()手动设置RadGrid中的DataSource,并对反射和属性映射进行手动排序:

代码语言:javascript
复制
public static Dictionary<string, PropertyInfo> ColumnMapper = new Dictionary<string, PropertyInfo>
                                                                   {
                                                                       {"StatusTypeId", typeof(Defects).GetProperty("CustomerStatus*") },
                                                                       {"DefectId", typeof(Defects).GetProperty("DefectId") },
                                                                       {"Name", typeof(Defects).GetProperty("Name") }
                                                                   }; 

* Thats the Custom Column from above.

使用以下代码将MasterTemplateView.AllowCustomSorting设置为True并对Page_Load()和SortCommand进行排序:

代码语言:javascript
复制
protected new void Page_Load(object sender, EventArgs e)
{
   var exp = this.RadGrid.MasterTableView.SortExpressions; 
   if (exp.Count == 0)
   {
      this.SortGrid(new GridSortExpression { FieldName = "DefectId", SortOrder = GridSortOrder.Ascending});
   }
   else
   {
      this.SortGrid(exp[0]);
   }
}

protected void SortCommand(object sender, GridSortCommandEventArgs e)
{
    this.SortGrid(new GridSortExpression { FieldName = e.SortExpression, SortOrder = e.NewSortOrder });
}

private void SortGrid(GridSortExpression e)
{
    IOrderedEnumerable<Defects> bindingList = null;
    var defects = DataAccessLayer.GetLoadedSet<Defects>().ToList();
    if (Defects.ColumnMapper.ContainsKey(e.FieldName))
    {
        var prop = Defects.ColumnMapper[e.FieldName];
        switch (e.SortOrder)
        {
        case GridSortOrder.Ascending:
            bindingList = defects.OrderBy(src => prop.GetValue(src, null));
            break;
        case GridSortOrder.Descending:
            bindingList = defects.OrderByDescending(src => prop.GetValue(src, null));
            break;
        case GridSortOrder.None:
            bindingList = defects.OrderBy(src => src.DefectId);
            break;
        }
    }
    this.RadGrid.DataSource = bindingList;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13974026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档