简体   繁体   English

使用linq排序网格视图列

[英]Sorting grid view column using linq

I am binding data to gridview in template field like : 我将数据绑定到模板字段中的gridview,如:

  <asp:TemplateField HeaderText="Business Objective" SortExpression="BusinessObjective.BusinessObjectiveText">
     <ItemTemplate>
        <asp:Label ID="lblBusinessObjectiveText" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "BusinessObjective.BusinessObjectiveText") %>'></asp:Label>
     </ItemTemplate>
  </asp:TemplateField>

In code behind I just bind list to grid view: 在后面的代码中,我只是将列表绑定到网格视图:

    gvBussinessRisks.DataSource = this.BusinessRiskList;
    gvBussinessRisks.DataBind();

List is of Type "BusinessRisk". 列表的类型为“ BusinessRisk”。 Entity structure is like 实体结构就像

public class BusinessRisk
{
    public BusinessRisk()
    {
        BusinessObjective BusinessObjective = new BusinessObjective();
    }

    public int? BusinessRiskId { get; set; }
    public int ObjectiveId { get; set; }
    public string BusinessRiskText { get; set; }
    public string Description { get; set; }
    public bool IsActive { get; set; }
    public long ActionedBy { get; set; }
    public long? DelegatedTo { get; set; }
    public int? LastLogId { get; set; }
    public int AppOwnerId { get; set; }

    public BusinessObjective BusinessObjective { get; set; }
}

My BusinessObjective class has property named "BusinessObjectiveText" Which I am binding to one of the column in grid view. 我的BusinessObjective类具有名为“ BusinessObjectiveText”的属性,该属性将绑定到网格视图中的某一列。

But when I am trying to sort this column i get error. 但是,当我尝试对此列进行排序时,我得到了错误。

My Sorting event is like: 我的排序事件类似于:

List<BusinessRisk> sortedList = this.BusinessRiskList;

    sortedList.Sort(new GenericComparer<BusinessRisk>(e.SortExpression, (SortDirection)Enum.Parse(typeof(SortDirection), GetSortDirection(e.SortExpression))));
    gvBussinessRisks.DataSource = sortedList;
    gvBussinessRisks.DataBind();

And "GenericComparer" class is: 而“ GenericComparer”类是:

public class GenericComparer<T> : IComparer<T>
{
    private string sortExpression;
    private SortDirection sortDirection;
    public GenericComparer(string sortExpression, SortDirection sortDirection)
    {
        this.sortExpression = sortExpression;
        this.sortDirection = sortDirection;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo propertyInfo = typeof(T).GetProperty(sortExpression);
        IComparable obj1 = (IComparable)propertyInfo.GetValue(x, null);
        IComparable obj2 = (IComparable)propertyInfo.GetValue(y, null);


        if (SortDirection == SortDirection.Ascending)
        {
            if (obj1 != null)
                return obj1.CompareTo(obj2);
            return 0;
        }
        else
        {
            if (obj1 != null)
                return obj2.CompareTo(obj1);
            return 0;
        }
    }

    public SortDirection SortDirection
    {
        get { return this.sortDirection; }
        set { this.sortDirection = value; }
    }


}

I get error when I am trying to sort this column as in GenericComparer class (method: Compare) I get propertyInfo as null . 当我尝试按GenericComparer类中的方法对此列进行排序时出现错误(方法:比较),我将propertyInfo获取为null If i am sorting using properties of BusinessRisk then sorting happens properly, but over here i am trying to sort using one of the property of BussinessObjective class. 如果我使用BusinessRisk属性进行排序,则排序会正确进行,但是在这里,我尝试使用BussinessObjective类的属性之一进行排序。

I tried Google, but not getting any solution. 我尝试了Google,但没有得到任何解决方案。

Finally after Lot of Google and little brainstorm I got solution, using Dynamic Linq . 终于,经过Google的大量努力和小小的头脑风暴,我得到了使用Dynamic Linq解决方案。

I ditched GenericComparer class, and just wrote following code in Sorting event 我放弃了GenericComparer类,只是在Sorting事件中编写了以下代码

string sortColumn = e.SortExpression;
    IQueryable<BusinessRisk> sortedList = (from p in this.BusinessRiskList
                                           select new BusinessRisk
                                           {
                                               BusinessRiskText = p.BusinessRiskText,
                                               IsActive = p.IsActive,
                                               Description = p.Description,
                                               BusinessObjective = new BusinessObjective { BusinessObjectiveText = p.BusinessObjective.BusinessObjectiveText }
                                           }).AsQueryable().OrderBy(e.SortExpression);




    this.BusinessRiskList = sortedList.ToList<BusinessRisk>();
    gvBussinessRisks.DataSource = sortedList;
    gvBussinessRisks.DataBind();

And its working perfect for me. 它对我来说非常完美。

Thank you everybody who tried helping me. 谢谢所有尝试帮助我的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM