RadGrid覆盖EntityDataSource排序

本文关键字:排序 EntityDataSource 覆盖 RadGrid | 更新日期: 2023-09-27 17:49:15

之前:

我使用的是一个静态数据库,它不能更改,因为它被外部软件使用。

如果数据库值不是"1"或"6",我会将它从StatusTypeId映射到介于这些值之间的值。背景:支持票据系统

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

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

在使用网格的排序函数之前,在EntityDataSource上执行的初始排序表达式运行良好。然后,无论我是手动拦截,排序都是错误的。

EntityDataSource:的初始排序

<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 Event(中获取请求的排序顺序,并将EntityDataSource的OrderBy属性设置为正确排序?我试过了,但被忽略了。

SortCommand事件的实现:

    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();
        }
    }

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

RadGrid覆盖EntityDataSource排序

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

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上使用以下代码进行排序:

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;
}