使用linq排序网格视图列
本文关键字:视图 网格 排序 linq 使用 | 更新日期: 2023-09-27 18:11:22
我将数据绑定到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();
清单类型为"商业风险"。实体结构类似于
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; }
}
我的businessoobjectject类有一个名为"businessobjecttext"的属性,我将它绑定到网格视图中的一个列。
但是当我试图排序这一列时,我得到错误。
我的排序事件如下:
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
的属性进行排序,那么排序就会正确发生,但在这里,我试图使用bussinessoobjective类的属性之一进行排序。
我试了谷歌,但没有得到任何解决方案。
最后,经过大量的谷歌搜索和头脑风暴,我找到了解决方案,使用动态Linq。
我抛弃了GenericComparer类,只在排序事件
中编写了以下代码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();
这对我来说是完美的。
感谢所有帮助我的人。