1

我将数据绑定到模板字段中的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>

在后面的代码中,我只是将列表绑定到网格视图:

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

列表的类型为“BusinessRisk”。实体结构就像

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

我的 BusinessObjective 类具有名为“BusinessObjectiveText”的属性,我将其绑定到网格视图中的列之一。

但是当我试图对这个列进行排序时,我得到了错误。

我的排序事件是这样的:

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

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


}

当我尝试将此列排序为GenericComparer类(方法:比较)时,我得到错误,我propertyInfo 得到null. 如果我使用的属性进行排序,BusinessRisk那么排序会正确进行,但是在这里我尝试使用 BussinessObjective 类的属性之一进行排序。

我尝试了谷歌,但没有得到任何解决方案。

4

1 回答 1

1

最后经过大量谷歌和小头脑风暴后,我得到了解决方案,使用Dynamic Linq

我放弃了 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();

它对我来说非常完美。

感谢所有试图帮助我的人。

于 2013-04-29T09:11:25.970 回答