default.aspx:
< asp : datagrid id="grd" runat="server" allowpaging="true" pagesize="4" allowsorting="true" autogeneratecolumns="false" onpageindexchanged="grd_PageIndexChanged" onsortcommand="grd_SortCommand">
< columns >
< asp : boundcolumn headertext="ID" datafield="ID" sortexpression="ID" />
< asp : boundcolumn headertext="Name" datafield="Name" sortexpression="Name" />
< asp : boundcolumn headertext="Marks" datafield="Marks" sortexpression="Marks" />
< / columns >
< pagerstyle horizontalalign="Center" mode="NextPrev" />
default.aspx.cs: (Code behind file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Data_Controls
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FillGrid();
if (!Page.IsPostBack)
{
FillColumnsSortDirectionList();
}
SetPagingSorting();
}
private void FillGrid()
{
DataView view = LoadView();
this.grd.DataSource = view;
this.grd.DataBind();
}
private void FillColumnsSortDirectionList()
{
DataView view = (DataView)this.grd.DataSource;
ListColumnsSortDirectionList = new List ();
foreach (DataColumn column in view.ToTable().Columns)
ColumnsSortDirectionList.Add(new Pair(column.ColumnName, "asc"));
Session.Add("ColumnsSortDirectionList", ColumnsSortDirectionList);
Session.Add("SortColumn", "ID");
}
private void SetPagingSorting()
{
((DataView)this.grd.DataSource).Sort = Session["SortColumn"].ToString() + " " +
Utility.PairList__GetValue((List)Session["ColumnsSortDirectionList"], Session["SortColumn"].ToString());
}
private DataTable LoadTable()
{
DataTable tab = new DataTable();
tab.Columns.Add("ID", typeof(int));
tab.Columns.Add("Name", typeof(string));
tab.Columns.Add("Marks", typeof(double));
tab.Rows.Add(new object[] { 1, "Atif", 85.40d });
tab.Rows.Add(new object[] { 2, "Jamal", 70.00d });
tab.Rows.Add(new object[] { 3, "Nawaz", 60.00d });
tab.Rows.Add(new object[] { 4, "Salma", 75.00d });
tab.Rows.Add(new object[] { 5, "Yasir", 78.00d });
tab.Rows.Add(new object[] { 6, "Shabnam", 50.00d });
tab.Rows.Add(new object[] { 7, "Naseem", 74.32d });
tab.Rows.Add(new object[] { 8, "Tauseef", 58.00d });
tab.Rows.Add(new object[] { 9, "Nasreen", 18.00d });
tab.Rows.Add(new object[] { 10, "Sadiq", 90.00d });
return tab;
}
private DataView LoadView()
{
return LoadTable().DefaultView;
}
protected void grd_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.grd.CurrentPageIndex = e.NewPageIndex;
this.grd.DataBind();
}
protected void grd_SortCommand(object source, DataGridSortCommandEventArgs e)
{
Session["SortColumn"] = e.SortExpression;
ColumnsSortDirectionList__ToggleValue(e.SortExpression);
((DataView)this.grd.DataSource).Sort = e.SortExpression + " " +
Utility.PairList__GetValue((List)Session["ColumnsSortDirectionList"], e.SortExpression);
this.grd.DataBind();
}
private void ColumnsSortDirectionList__ToggleValue(string _Name)
{
ListColumnsSortDirectionList = (List )Session["ColumnsSortDirectionList"];
foreach (Pair p in ColumnsSortDirectionList)
{
if (p.Name == _Name)
{
if (p.Value == "asc")
p.Value = "desc";
else if (p.Value == "desc")
p.Value = "asc";
break;
}
}
}
}
}
Pair.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Data_Controls
{
public class Pair
{
public string Name;
public string Value;
public Pair(string _Name, string _Value)
{
this.Name = _Name;
this.Value = _Value;
}
}
}
Utility.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Data_Controls
{
public class Utility
{
public static string PairList__GetName(List_PairList, string _Value)
{
string returnValue = "";
foreach (Pair p in _PairList)
{
if (p.Value == _Value)
{
returnValue = p.Name;
break;
}
}
return returnValue;
}
public static string PairList__GetValue(List_PairList, string _Name)
{
string returnValue = "";
foreach (Pair p in _PairList)
{
if (p.Name == _Name)
{
returnValue = p.Value;
break;
}
}
return returnValue;
}
}
}
24 August, 2010
Golden Code: Maintain Sorting While Moving to Next Page In Data Grid
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment