|
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace SortingAndPaging
{
public partial class CountryView : System.Web.UI.Page
{
#region Page Load Event
/// <summary>
/// On Page Load:Sort Country Ascending Order/Sort Column: CountryID
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (IsPostBack == false)
{
SortColumn = Country.CountryComparer.CountryComparisonType.CountryID;
SortDirection = Utility.SortOrder.Asc;
LoadCountryList();
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// OnRowCreated Event: Add 'Ascending' and 'Descending' image in Grid Header Control.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridCountry_RowCreated(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell tableCell in e.Row.Cells)
{
if (tableCell.HasControls())
{
LinkButton columnLinkButton = (LinkButton)tableCell.Controls[0];
if (columnLinkButton != null)
{
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
img.ImageUrl = "~/Image/" + this.SortDirection.ToString() + ".gif";
if (SortColumn == (Country.CountryComparer.CountryComparisonType)Enum.Parse(typeof(Country.CountryComparer.CountryComparisonType), columnLinkButton.CommandArgument))
{
tableCell.Controls.Add(new LiteralControl(" "));
tableCell.Controls.Add(img);
}
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region Grid Events
/// <summary>
/// OnPageIndexChanging Event : Load Country List based on Current Sort Order and Sort Column.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridCountry_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
gridCountry.PageIndex = e.NewPageIndex;
PagingCountryList();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// OnSorting Event: Load and Sort Country List based on Column and Order selected.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridCountry_Sorting(object sender, GridViewSortEventArgs e)
{
try
{
SortCountryList((Country.CountryComparer.CountryComparisonType)Enum.Parse(typeof(Country.CountryComparer.CountryComparisonType), e.SortExpression));
}
catch(Exception ex )
{
throw ex;
}
}
#endregion
#region Private Method
/// <summary>
/// Bind Grid with Country List DataSource.
/// </summary>
/// <param name="list"></param>
private void BindGrid<T>(T list)
{
gridCountry.DataSource = list;
gridCountry.DataBind();
}
/// <summary>
/// Called By Grid Page Event
/// </summary>
private void PagingCountryList()
{
SortCountryList(Country.CountryComparer.CountryComparisonType.NULL);
}
/// <summary>
/// Called By PageLoad Event
/// </summary>
private void LoadCountryList()
{
SortCountryList(Country.CountryComparer.CountryComparisonType.NULL);
}
/// <summary>
/// Called By Grid Sort Event
/// </summary>
/// <param name="sortExpression"></param>
private void SortCountryList(Country.CountryComparer.CountryComparisonType sortExpression)
{
List<Country> countryList = (List<Country>)GetCountryList();
Country.CountryComparer countryComparer = new Country.CountryComparer();
if (Country.CountryComparer.CountryComparisonType.NULL != sortExpression)
{
if (sortExpression == SortColumn)
{
if (SortDirection == Utility.SortOrder.Asc)
{
SortDirection = Utility.SortOrder.Desc;
}
else
{
SortDirection = Utility.SortOrder.Asc;
}
}
else
{
SortDirection = Utility.SortOrder.Asc;
}
SortColumn = sortExpression;
}
countryComparer.WhichComparison = SortColumn;
countryComparer.SortDirection = SortDirection;
countryList.Sort(countryComparer);
CountryDataSource = countryList;
}
/// <summary>
/// Fetch Data from database and fill country collection list.
/// </summary>
/// <returns></returns>
private IList<Country> GetCountryList()
{
IList<Country> countryList = new List<Country>();
countryList.Add(new Country(1, "United States Of America", 100001));
countryList.Add(new Country(2, "Africa", null));
countryList.Add(new Country(3, "India", null));
countryList.Add(new Country(4, "Singapore", 100004));
countryList.Add(new Country(5, "Newzealand", 100005));
countryList.Add(new Country(6, "United Kingdom", 100006));
countryList.Add(new Country(7, "Australia", 100007));
countryList.Add(new Country(8, "China", 100008));
countryList.Add(new Country(9, "Malaysia", null));
countryList.Add(new Country(10, "Germany", 100011));
countryList.Add(new Country(11, "France", 100009));
countryList.Add(new Country(12, "United States Of Soviet Russia", null));
return countryList;
}
#endregion
#region Private Property
/// <summary>
/// Property CountryDataSource
/// </summary>
private IList<Country> CountryDataSource
{
get { return (List<Country>)gridCountry.DataSource; }
set { BindGrid(value);}
}
/// <summary>
/// Property SortColumn
/// </summary>
private Country.CountryComparer.CountryComparisonType SortColumn
{
get { return (Country.CountryComparer.CountryComparisonType)ViewState["SORT_EXP"]; }
set { ViewState["SORT_EXP"] = value; }
}
/// <summary>
/// Property SortDirection
/// </summary>
private Utility.SortOrder SortDirection
{
get { return (Utility.SortOrder)ViewState["SORT_ORDER"]; }
set { ViewState["SORT_ORDER"] = value; }
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.