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 ValidatorDataGrid
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid MyDataGrid;
		protected System.Web.UI.WebControls.Label Label1;
		
		DataTable Cart;
		DataView CartView;
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (Session["ShoppingCart"] == null)     
			{
				Cart = new DataTable();
				Cart.Columns.Add(new DataColumn("Qty", typeof(string)));
				Cart.Columns.Add(new DataColumn("Item", typeof(string)));
				Cart.Columns.Add(new DataColumn("Price", typeof(string)));
				Session["ShoppingCart"] = Cart;

				// 第一次加载 -- 预填充一些数据
				for (int i=1; i<5; i++) 
				{
					DataRow dr = Cart.NewRow();
					dr[0] = ((int)(i%2)+1).ToString();
					dr[1] = "项 " + i.ToString();
					dr[2] = ((double)(1.23 * (i+1))).ToString();
					Cart.Rows.Add(dr);
				}
			}
			else 
			{
				Cart = (DataTable)Session["ShoppingCart"];
			}

			CartView = new DataView(Cart);
			CartView.Sort = "Item";
			if (!IsPostBack) 
			{
				BindGrid();
			}
		}

		public void BindGrid() 
		{
			MyDataGrid.DataSource = CartView;
			MyDataGrid.DataBind();
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.MyDataGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_CancelCommand);
			this.MyDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_EditCommand);
			this.MyDataGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_UpdateCommand);
			this.MyDataGrid.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.MyDataGrid_ItemDataBound);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void MyDataGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			// 对于绑定列，编辑的值存储在文本框中，
			// 文本框是列单元格中的第零个元素
			string item = e.Item.Cells[1].Text;
			string qty = ((TextBox)e.Item.Cells[2].FindControl("txtBox")).Text;
			string price = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
			// 对于数据库，我们应使用一条更新命令。因为我们使用
			// 内存内数据表，所以我们将删除旧行并用新行替换它
			//移除旧项
			CartView.RowFilter = "Item='"+item+"'";
			if (CartView.Count > 0) 
			{
				//项在 cart 中
				CartView.Delete(0);
			}
			CartView.RowFilter = "";

			//添加新项
			DataRow dr = Cart.NewRow();
			dr[0] = qty;
			dr[1] = item;
			dr[2] = price;
			Cart.Rows.Add(dr);
			MyDataGrid.EditItemIndex = -1;
			BindGrid();
		}

		private void MyDataGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			MyDataGrid.EditItemIndex = -1;
			BindGrid();
		}

		private void MyDataGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			MyDataGrid.EditItemIndex = (int)e.Item.ItemIndex;
			BindGrid();
		}

		private void MyDataGrid_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			if( e.Item.ItemType == ListItemType.EditItem )
			{
				RequiredFieldValidator re = new RequiredFieldValidator();
				re.ControlToValidate = "txtBox";
				re.Text = "*";
				re.Display = ValidatorDisplay.Dynamic;
				re.ErrorMessage = "请输入数字";
				e.Item.Cells[2].Controls.Add(re);
				
				RangeValidator rerange = new RangeValidator();	
				rerange.ControlToValidate = "txtBox";
				rerange.Type = ValidationDataType.Integer;
				rerange.Text = "*";
				rerange.Display = ValidatorDisplay.Dynamic;
				rerange.MaximumValue = "100";
				rerange.MinimumValue = "1";
				rerange.ErrorMessage = "请输入1到100的整数";				
				e.Item.Cells[2].Controls.Add(rerange);

				ValidationSummary summary = new ValidationSummary();
				summary.ShowMessageBox = true;
				summary.ShowSummary = false;
				e.Item.Cells[2].Controls.Add(summary);
			}
		}
	}
}
