I am binding data to gridview in template field like :
<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". 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.
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:
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
. 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.
I tried Google, but not getting any solution.
Finally after Lot of Google and little brainstorm I got solution, using Dynamic Linq .
I ditched GenericComparer class, and just wrote following code in Sorting event
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.