I am binding ASP.Net Grid View with list object. I have created Country and City Class inherited from their respective interface.
public interface ICountry
{
int cmID { get; set; }
string cmName { get; set; }
}
public interface ICity
{
int ctyId { get; set; }
string ctyName { get; set; }
}
public class Country : ICountry
{
public int cmID { get; set; }
public string cmName { get; set; }
}
public class City : ICity
{
public int ctyId { get; set; }
public string ctyName { get; set; }
public ICountry Country { get; set; }
public List<City> GetAllCity(string SortDirection, string SortExpression)
{
DataTable dt = FillCity() //returns city,country in a table
List<City> obj = new List<City>();
foreach(DataRow dr in dt.Rows)
{
City o = new City();
o.ctyName = dr["ctyName"].ToString();
o.Country.cmName = dr["cmName"].ToString();
obj.Add(o);
}
dt.Dispose();
return obj;
}
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false" PageSize="15"
AllowPaging="true" OnPageIndexChanging="GRV1_PageIndexChanging"
AllowSorting="true" onsorting="GRV1_Sorting">
<Columns>
<asp:TemplateField ItemStyle-Width="10%" ItemStyle-HorizontalAlign="Left" HeaderText="City" SortExpression="ctyName ">
<ItemTemplate>
<%# Eval("ctyName")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width="15%" ItemStyle-HorizontalAlign="Left" HeaderText="Country" SortExpression="Country.cmName">
<ItemTemplate>
<%# Eval("Country.cmName")%>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView>
I have tried to sort using Linq but failed to do so. How can I sort data in a grid view using List ?
I have tried using following but didn't work
if (obj != null)
{
var param = Expression.Parameter(typeof(MemorandaClosing), SortExpression);
var sortExpression = Expression.Lambda<Func<MemorandaClosing, object>>(Expression.Convert(Expression.Property(param, SortExpression), typeof(object)), param);
if (SortDirection == "ASC")
{
obj.AsQueryable<MemorandaClosing>().OrderBy(sortExpression);
}
else
{
obj.AsQueryable<MemorandaClosing>().OrderByDescending(sortExpression);
};
}