Datagrid Sorting
1) AllowSorting Property to true.
2) Set the SortExpression Property of individual column to their respective field name. Eg: EmpId, EmpName, etc.
3) Write Code on Event.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindGrid(fetchData());
ViewState["SortOrder"] = "ASC";
}
}
private void DataGrid1_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataView dv = new DataView(fetchData());
dv.Sort = e.SortExpression + " " + ViewState["SortOrder"];
BindGrid(dv);
if(ViewState["SortOrder"].ToString().ToUpper() == "ASC")
ViewState["SortOrder"] = "DESC";
else
ViewState["SortOrder"] = "ASC";
}
No comments:
Post a Comment