ASP Alliance   .Net Code   Useful Methods
bisort.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace olson.code.bisort
{
  public class bisort : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.DataGrid dgAthletes;
    protected string SortExpression;
    protected string SDirection;

    private void Page_Load(object sender, System.EventArgs e)
    {
      //Set DataGrid
      SetdgAthletes();
    }

    private void SetdgAthletes()
    {
      DataSet ds = new DataSet();
      string spath = Request.MapPath(Request.ApplicationPath);
      //fill the dataset with the data from athletes.xml
      ds.ReadXml( spath + "\\code\\bisort\\athletes.xml" );

      //make sure the two global variables have values
      SortExpression = (SortExpression == null) ? "AA" : SortExpression;
      SDirection = (SDirection == null) ? "ASC" : SDirection;
      //Response.Write(SortExpression + "|" + SDirection + "|");

      //Sort the data
      DataView dv = ds.Tables[0].DefaultView;
      dv.Sort = SortExpression + " " + SDirection;

      //Bind the data
      dgAthletes.DataSource = dv;
      dgAthletes.DataBind();
    }


    override protected void OnInit(EventArgs e)
    {
      InitializeComponent();
      base.OnInit(e);
    }

    private void InitializeComponent()
    {
      this.dgAthletes.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgAthletes_SortCommand);
      this.Load += new System.EventHandler(this.Page_Load);
    }

    private void dgAthletes_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
      //make sure the session variables have values
      if (Session["SortExpression"] == null)
      {
        Session["SortExpression"] = "AA";
        Session["SDirection"] = "ASC";
      }

      //change sort direction if the previous column sorted is the same as the current column sorted
      SortExpression = e.SortExpression;
      if ( SortExpression.Equals(Session["SortExpression"].ToString()) )
      {
        SDirection = (Session["SDirection"].ToString().StartsWith("ASC")) ? "DESC" : "ASC";
      }
      else
      {
        SDirection = "ASC";
      }

      //set the session variables to new value
      Session["SortExpression"] = SortExpression;
      Session["SDirection"] = SDirection;

      //Set Datagrid
      SetdgAthletes();
    }
  }
}
csharpindex.com/colorCode
Comments