using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 UsingServerControls
{
	/// <summary>
	/// Summary description for DataGridFeatures.
	/// </summary>
	public class DataGridFeatures : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid BookGrid;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Summary;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.TextBox KeyWord;
		protected System.Web.UI.WebControls.Label Label1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if (!Page.IsPostBack)
				BindDataGrid();
		}

		private void BindDataGrid()
		{
			SqlConnection myConnection = new SqlConnection("server=localhost;Trusted_Connection=true;database=NorthWind");
			string selectionString = "Select Title, Price from Books";
			SqlCommand myCommand = new SqlCommand(selectionString, myConnection);
			SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

			DataSet myDataSet = new DataSet();
			myAdapter.Fill(myDataSet, "Books");

			DataView myView = myDataSet.Tables[0].DefaultView;
			// get the sort string from ViewState
			if (null != this.ViewState["SortString"])
				myView.Sort = this.ViewState["SortString"].ToString();
			// get the filter string from ViewState
			if (null != this.ViewState["Filter"])
				myView.RowFilter = "Title LIKE '*" + this.ViewState["Filter"].ToString() + "*'";
			this.BookGrid.DataSource = myView;
			this.BookGrid.DataMember = "Books";
			this.BookGrid.DataBind();
		}

		private void ShowSummary(object sender, DataGridCommandEventArgs e)
			{
			// Check the command name. We only handler "ShowSummary"
			if ("ShowSummary" == e.CommandName)
				{
				this.Summary.Text = "Here is the summary for book " + e.Item.Cells[1].Text;
				BindDataGrid();
				}
			}

		private void OnItemDataBound(object sender, DataGridItemEventArgs e)
			{
			// get the book title
			if ((ListItemType.Item == e.Item.ItemType) || (ListItemType.AlternatingItem == e.Item.ItemType))
				{
				string title = e.Item.Cells[1].Text;
				// indicate this is an ASP.Net book
				if (title.IndexOf("ASP.Net") >= 0)
					{
					e.Item.Cells[0].Text = "ASP.Net book";
					}
				}
			}

		private void SortRows(object sender, DataGridSortCommandEventArgs e)
			{			
			if ((null != this.ViewState["SortString"]) && (this.ViewState["SortString"].ToString() == e.SortExpression.ToString()))
				{
				// if we are sorting using the same field, change the sorting order
				string s = this.ViewState["SortString"].ToString();
				int i = s.IndexOf(" ASC");
				if (i > 0)
					s = s.Replace(" ASC", " DESC");
				else
					{
						s = s.Replace(" DESC", " ASC");
					}
				// store the sort string in ViewState so that it can be restored during post back
				this.ViewState["SortString"] = s;
				}			
			else
				// store the sort string in ViewState
				this.ViewState["SortString"] = e.SortExpression.ToString();
			BindDataGrid();
			}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.BookGrid.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.ShowSummary);
			this.BookGrid.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortRows);
			this.BookGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnItemDataBound);
			this.KeyWord.TextChanged += new System.EventHandler(this.KeyWord_TextChanged);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void KeyWord_TextChanged(object sender, System.EventArgs e)
			{
			if ((null != this.ViewState["Filter"]) && (this.ViewState["Filter"].ToString()  == this.KeyWord.Text))
				return;

			// store the filter string in ViewState so that it can be restored during post back
			this.ViewState["Filter"] = this.KeyWord.Text;
			BindDataGrid();
			}
	}
}
