ASP.NET GridView Sorting
Process 1 :
protected void gvUser_Sorting(object sender, GridViewSortEventArgs e)
{
string search = txtSearch.Text.Trim();
string SortExp = e.SortExpression;
List<Common.TblUser> oList = UserBLL.GetUserList();
if (txtSearch.Text != "")
{
string text = txtSearch.Text.ToUpper();
oList = oList.FindAll(m => m.FullName.ToUpper().Contains(text) || m.RoleName.ToUpper().Contains(text) || m.UserName.ToUpper().Contains(text) || m.ExternalID.ToUpper().Contains(text)).ToList();
}
System.Reflection.PropertyInfo property = oList.GetType().GetGenericArguments()[0].GetProperty(SortExp);
if (e.SortDirection == SortDirection.Ascending)
{
oList = oList.OrderBy(g => property.GetValue(g, null)).ToList<Common.TblUser>();
}
else
{
oList = oList.OrderByDescending(g => property.GetValue(g, null)).ToList<Common.TblUser>();
}
this.gvUser.DataSource = oList;
this.gvUser.DataBind();
}
Process 2
Using Object Datasource
<asp:GridView ID="GVUserList" runat="server" AutoGenerateColumns="False"
DataSourceID="odsGrid" AllowPaging="True" AllowSorting="True"
DataKeyNames="CBTID" >
<Columns>
<asp:BoundField DataField="BTName" HeaderText="BTName" SortExpression="BTName" />
</Columns>
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<th>No Record Added</th>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
<asp:ObjectDataSource ID="odsGrid" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="SelectAllForGrid" TypeName="WellFood.Business.Managers.BrandTypeManager"
SortParameterName="Sortby">
<SelectParameters>
<asp:Parameter Name="SearchExpression" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
[DataObjectMethod(DataObjectMethodType.Select)]
public List<BrandType> SelectAllForGrid(string SearchExpression, string Sortby)
{
if (string.IsNullOrEmpty(Sortby))
Sortby = "PrdID";
Query = @"SELECT ROW_NUMBER() OVER(ORDER BY " + Sortby + @" ) as RowNum,
CBTID ,BTID ,BTName ,PrdID ,
PrdName ,GroupID ,GroupName ,FloorID ,VATPrcnt ,DiscPrcnt
FROM [BrandType]
order by RowNum";
oR = sQLDal.Select(Query);
List<BrandType> oList = new List<BrandType>();
if (oR.ResultState && oR.Data.Rows.Count > 0)
{
oList = DataTableToClass.DataTableToList<BrandType>(oR.Data);
return oList;
}
else return null;
}