<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@Page Debug=True %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="C#" runat="server">
	SqlConnection Source;
	string d="SELECT Orders.OrderID, Orders.CusID, Customers.CusName,Orders.OrderDate, Orders.BeforeTaxPrice, Orders.TotalPrice, Orders.TaxPrice, Orders.Tax FROM Orders INNER JOIN Customers ON Orders.CusID = Customers.CusID";
	int NowRecord;//目前条数
	void Page_load(object sender,EventArgs e){
		Source = (SqlConnection)Session["NowSTKSQL"];
		if(IsPostBack==false){
			NowRecord = 0; //取得第一条记录
			Status.Text="查看记录";
			StatusSub.Text="查看记录";
			bindNew(true);
			}
	}
	void bindNew(Boolean tag){
		if(tag==true){
			DataSet DS = new DataSet();
			SqlDataAdapter NComm = new SqlDataAdapter(d, Source);
			NComm.Fill(DS, "Orders");
			NComm.Fill(DS, "OrdersN");
			DS.Tables["OrdersN"].Rows.Clear();
			Session["NowDO"]=DS;//声明为Session
			ShowRecord(); //执行显示记录
		}
		else{
			DataSet DS=(DataSet)Session["NowDO"];
			Orders.DataSource=DS.Tables["OrdersN"].DefaultView;
			Orders.DataBind();
		}
	}
	void ShowRecord(){
		DataSet NowDS = (DataSet)Session["NowDO"];
		NowDS.Tables["OrdersN"].Rows.Clear();
		if(NowRecord >= 0){ //非添加时
			DataRow NRow = NowDS.Tables["Orders"].Rows[NowRecord];
			NowDS.Tables["OrdersN"].ImportRow(NRow);
			Orders.DataSource=NowDS.Tables["OrdersN"].DefaultView;
			Orders.DataBind();
			CurrentRecord.Text=(NowRecord+1).ToString();
			AllRecords.Text=NowDS.Tables["Orders"].Rows.Count.ToString();
		}
		else{
			DataSet DS = (DataSet)Session["NowDO"];
			NowDS.Tables["OrdersN"].Rows.Clear();//清除记录
			DataRow NwRow=DS.Tables["OrdersN"].NewRow();//添加
			NwRow["Tax"]=0;//为各栏指定预设值
			NwRow["TaxPrice"]=0;
			NwRow["BeforeTaxPrice"]=0;
			NwRow["TotalPrice"]=0;
			DS.Tables["OrdersN"].Rows.Add(NwRow);
			Orders.DataSource=NowDS.Tables["OrdersN"].DefaultView;
			Orders.EditItemIndex=0;
			Orders.DataBind();
			Control k=(Control)Orders;
			DropDownListSource(k,"DataList","CusName","CusID",0,"Customers","CusID",0,"","CusID");
		}
		DetailBind(true);
	}
	void DetailBind(Boolean tag){
		if(tag==true){
			DataSet NowDS = (DataSet)Session["NowDO"];
			string k="SELECT Product.ProductName, OrderDetail.ID, OrderDetail.OrderID, ";
			k+="OrderDetail.ProductID, OrderDetail.Price, OrderDetail.Quantity, ";
			k+="OrderDetail.TotalThis, OrderDetail.ToStock FROM OrderDetail INNER ";
			k+=" JOIN Product ON OrderDetail.ProductID = Product.ProductID ";
			k+="where OrderID ='" + NowDS.Tables["OrdersN"].Rows[0]["OrderID"] + "'";
			SqlDataAdapter NComm1 = new SqlDataAdapter(k, Source);
			try{//若是第一次载入，会发生错误，以try避开
				NowDS.Tables["OrderDetail"].Rows.Clear();//清除记录
			}
			catch{}
			NComm1.Fill(NowDS, "OrderDetail");
			Detail.DataSource=NowDS.Tables["OrderDetail"].DefaultView;
			Detail.DataBind();
		}
		else{
			DataSet NowDS = (DataSet)Session["NowDO"];
			Detail.DataSource=NowDS.Tables["OrderDetail"].DefaultView;
			Detail.DataBind();
		}
	}
	void ChangeRecord(object sender, CommandEventArgs e){
		if(e.CommandArgument == "Next"){//下条
			if(CurrentRecord.Text==AllRecords.Text){
				NowRecord = Convert.ToInt32(AllRecords.Text)-1;
			}
			else{
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text)+1).ToString();
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
			}
		}
		else if(e.CommandArgument == "First"){//首条
			CurrentRecord.Text="1";
			NowRecord = 0;
		}
		else if(e.CommandArgument == "Last"){//末条
			CurrentRecord.Text=(Convert.ToInt32(AllRecords.Text)).ToString();
			NowRecord = Convert.ToInt32(AllRecords.Text) - 1;
		}
		else if(e.CommandArgument == "Prev"){//上条
			if(Convert.ToInt32(CurrentRecord.Text) > 1){
				CurrentRecord.Text=(Convert.ToInt32(CurrentRecord.Text) - 1).ToString();
				NowRecord = Convert.ToInt32(CurrentRecord.Text)-1;
			}
			else{
				NowRecord = 1;
			}
		}
		ShowRecord();
		DetailBind(true);
	}
	void GotoRecord(Object sender,EventArgs e){
		try{
			int k = Convert.ToInt32(ToRecord.Text);//取得输入之数字
			if(k>Convert.ToInt32(AllRecords.Text)){//若输入大于总条数
				k = Convert.ToInt32(AllRecords.Text);
			}
			if(k<0){//若输入小于0
				k = 1;
			}
			NowRecord=k-1;
			ToRecord.Text=k.ToString();
			CurrentRecord.Text=k.ToString();
			ShowRecord();
		}
		catch{
			NowRecord=0;
			ToRecord.Text="1";
			CurrentRecord.Text="1";
			ShowRecord();
		}
	}
	void Orders_Edit(object sender,DataListCommandEventArgs e){
		Orders.EditItemIndex = e.Item.ItemIndex;
		bindNew(false);
		TextBox f=(TextBox)Orders.Items[0].FindControl("OrderID");
		f.Visible=false;
		DataSet DS = (DataSet)Session["NowDO"];
		DataRow NRow = DS.Tables["OrdersN"].Rows[0];
		string h=NRow["CusID"].ToString();
		Control k=(Control)Orders;
		AddOrder.Visible=false;
		AddDetail.Visible=false;
		DropDownListSource(k,"DataList","CusName","CusID",0,"Customers","CusID",0,h,"CusID");
		Status.Text="编辑记录";
		ChangeDetailEdit(false);
	}
	void ChangeDetailEdit(Boolean tag){
		for(int i=0;i<=Detail.Items.Count-1;i++){
			Detail.Items[i].Cells[0].Controls[0].Visible=tag;
			Detail.Items[i].Cells[1].Controls[0].Visible=tag;
		}
	}
	Double Teststring(string h){
		if(h==""){
			return 0;
		}
		else{
			return Convert.ToDouble(h);
		}
	}
	void Orders_Update(object sender,DataListCommandEventArgs e){
		DataSet DS = (DataSet)Session["NowDO"];
		string w="";
		TextBox OrderDate = (TextBox)e.Item.FindControl("OrderDate");
		TextBox BeforeTaxPrice = (TextBox)e.Item.FindControl("BeforeTaxPrice");
		TextBox TaxPrice = (TextBox)e.Item.FindControl("TaxPrice");
		TextBox TotalPrice = (TextBox)e.Item.FindControl("TotalPrice");
		Double k=Convert.ToDouble(TaxPrice.Text) + Convert.ToDouble(BeforeTaxPrice.Text);//取得含税价
		DropDownList dt=(DropDownList)e.Item.FindControl("CusID");
		int y=0;
		if(TaxPrice.Text!="0"){//若不等于0则y等于1
			y=1;//y是含税否
		}
		if(Status.Text=="编辑记录"){
			Label NowID = (Label)e.Item.FindControl("OrderIDV");
			w = "Update Orders set";
			w += " OrderDate ='" + OrderDate.Text + "', BeforeTaxPrice =" + Teststring(BeforeTaxPrice.Text);
			w += ", TaxPrice =" + Teststring(TaxPrice.Text) + ", TotalPrice =" + k;
			w += ", Tax = " + y + ",CusID ='" + dt.SelectedItem.Value + "' where OrderID ='" + NowID.Text + "'";
		}
		else{
			TextBox Nowid = (TextBox)e.Item.FindControl("OrderID");
			w = "Insert Into Orders (OrderID,OrderDate,BeforeTaxPrice,Tax,TaxPrice,TotalPrice,CusID) Values ( '";
			w += Nowid.Text + "','" + OrderDate.Text + "'," + Teststring(BeforeTaxPrice.Text) + "," + y + "," + Teststring(TaxPrice.Text);
			w += "," + Teststring(TotalPrice.Text) + ",'" + dt.SelectedItem.Value + "')";
		}
		try{
			SqlCommand UpOrder=new SqlCommand(w,Source);
			UpOrder.Connection.Open();
			UpOrder.ExecuteNonQuery();//执行保存
			UpOrder.Connection.Close();
			Orders.EditItemIndex = -1;
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1;
			Orders.EditItemIndex = -1;
			NowRecord=Convert.ToInt32(CurrentRecord.Text)-1;
			bindNew(true);
			AddOrder.Visible=true;
			AddDetail.Visible=true;
			ChangeDetailEdit(true);
			Status.Text="查看记录";
		}
		catch(Exception p){
			errMsg.Text="以" + w + "添加失败" + p.ToString();
		}
	}
	void Orders_Cancel(object sender,DataListCommandEventArgs e){
		if(Status.Text=="添加记录"){
			Status.Text="查看记录";
			int x=Convert.ToInt32(AllRecords.Text);
			NowRecord=x-2;
			AllRecords.Text=(x-1).ToString();
			CurrentRecord.Text=(x-1).ToString();
			ShowRecord();
		}
		Orders.EditItemIndex = -1;
		AddOrder.Visible=true;
		AddDetail.Visible=true;
		bindNew(false);
		ChangeDetailEdit(true);
	}
	void AddOrder_Click(Object sender,EventArgs e){
		NowRecord=-1;
		ShowRecord();
		Status.Text="添加记录";
		Label f=(Label)Orders.Items[0].FindControl("OrderIDV");
		f.Visible=false;//隐藏
		int x=Convert.ToInt32(AllRecords.Text)+1;
		AllRecords.Text=x.ToString();//总条数加1
		CurrentRecord.Text=x.ToString();
		AddOrder.Visible=false;
		DetailBind(true);
	}
	void AddDetail_Click(Object sender,EventArgs e){
		DataSet NowDS = (DataSet)Session["NowDO"];
		DataRow NwRow =	NowDS.Tables["OrderDetail"].NewRow();//声明新记录
		Label NowID = (Label)Orders.Items[0].FindControl("OrderIDL");
		NwRow["OrderID"]= NowID.Text;
		NwRow["Quantity"]=0;
		NwRow["Price"]=0;
		NwRow["TotalThis"]=0;
		NowDS.Tables["OrderDetail"].Rows.Add(NwRow);
		Detail.DataSource=NowDS.Tables["OrderDetail"].DefaultView;//结合
		Detail.DataBind();
		Detail.EditItemIndex=Detail.Items.Count-1;//编辑
		Detail.DataBind();
		Control h=(Control)Detail;
		DropDownListSource(h,"DataGrid","ProductName","ProductID",Detail.Items.Count-1,"Product","ProductID",2,"","ProductID");
		AddDetail.Visible=false;
		StatusSub.Text="添加记录";
	}
	void Detail_Cancel(object Sender,DataGridCommandEventArgs e){
		Detail.EditItemIndex=-1;
		DetailBind(true);
		AddDetail.Visible=true;
		StatusSub.Text="查看记录";
	}
	void DropDownListSource(Control Gd,string Type,string TextField,string ValueField,int NowIndex,string Sname,string ControlName,int CellPosition,string CompareValue,string CompareField){
		DropDownList dt=null;
		DataSet NowDS = (DataSet)Session["NowDO"];
		SqlDataAdapter NComm1  = new SqlDataAdapter("select * from " + Sname, Source);
		NComm1.Fill(NowDS, Sname);//取得来源
		if(Type=="DataGrid"){//判断传入之类型及寻找DropDownList
			DataGrid nc = (DataGrid)Gd;
			dt=(DropDownList)nc.Items[NowIndex].Cells[CellPosition].FindControl(ControlName);
		}
		else if(Type=="DataList"){
			DataList nc = (DataList)Gd;		
			dt=(DropDownList)nc.Items[NowIndex].FindControl(ControlName);
		}	
		dt.DataSource=NowDS.Tables[Sname].DefaultView;
		dt.DataTextField=TextField;
		dt.DataValueField=ValueField;
		dt.DataBind();
		if(CompareValue==""){//若未传入比对值
			dt.SelectedIndex=0; //选取第一条选项
			return;
		}
		int f=0;  //逐一比对
		foreach(DataRow Row in NowDS.Tables[Sname].Rows){
			if(Row[CompareField].ToString()==CompareValue){
				dt.SelectedIndex=f; //找到及设定选取索引值
				return;
			}
			f+=1;
		}
	}
	void Detail_Update(object Sender,DataGridCommandEventArgs e){
		string w="";
		DropDownList dt=(DropDownList)e.Item.Cells[2].FindControl("ProductID");
		TextBox Quantity=(TextBox)e.Item.Cells[3].FindControl("Quantity");
		TextBox Price=(TextBox)e.Item.Cells[4].FindControl("Price");
		Label TotalThis=(Label)e.Item.Cells[5].FindControl("TotalThis");
		Label NowID = (Label)Orders.Items[0].FindControl("OrderIDL");
		if(StatusSub.Text=="添加记录"){
			w="Insert Into OrderDetail (OrderID,ProductID,Quantity,Price,TotalThis) Values (";
			w += "'" + NowID.Text + "','" + dt.SelectedItem.Value + "'," + Quantity.Text + "," + Price.Text;
			w += "," + TotalThis.Text + ")";
		}
		else{
			int m=Convert.ToInt32(Detail.DataKeys[Detail.EditItemIndex]);
			w = "Update OrderDetail Set ProductID ='" + dt.SelectedItem.Value + "',Quantity =" + Quantity.Text;
			w+=",Price =" + Price.Text + ",TotalThis =" + TotalThis.Text + " where ID =" + m;
		}
		try{
			SqlCommand UpOrderDetail=new SqlCommand(w,Source);
			UpOrderDetail.Connection.Open();
			UpOrderDetail.ExecuteNonQuery();//执行保存
			UpOrderDetail.Connection.Close();
			Detail.EditItemIndex=-1;
			DetailBind(true);
			AddDetail.Visible=true;
			StatusSub.Text="查看记录";
			AddOrder.Visible=true;
			BeforeTaxCount();
//			ChangeDetailEdit(true);
		}
		catch(Exception g){
			errMsg.Text="发生错误，无法保存" + g.ToString();
		}
	}
	void Detail_Edit(object Sender,DataGridCommandEventArgs e){
		Label v=(Label)e.Item.Cells[1].FindControl("ProductIDL");
		string v1=v.Text;
		Detail.EditItemIndex=e.Item.ItemIndex;
		DetailBind(false);
		Control h=(Control)Detail;
		DropDownListSource(h,"DataGrid","ProductName","ProductID",e.Item.ItemIndex,"Product","ProductID",2,v1,"ProductID");
		AddDetail.Visible=false;
		StatusSub.Text="编辑记录";
	}
	void Qu_Changed(object sender, EventArgs e){
		TextBox Qu = (TextBox)Detail.Items[Detail.EditItemIndex].Cells[3].FindControl("Quantity");
		TextBox Pri = (TextBox)Detail.Items[Detail.EditItemIndex].Cells[4].FindControl("Price");
		Label SubT = (Label)Detail.Items[Detail.EditItemIndex].Cells[5].FindControl("TotalThis");
		try{
			SubT.Text=(Convert.ToInt32(Qu.Text) * Convert.ToInt32(Pri.Text)).ToString();
		}
		catch{
			SubT.Text="0";//发生错误
		}
	}
	Double BeforeTaxTotal(){
		Double Total=0;
		for(int i=0;i<=Detail.Items.Count-1;i++){
			Label SubT = (Label)Detail.Items[i].Cells[5].FindControl("TotalThis");
			try{
			Total += Convert.ToDouble(SubT.Text);
			}
			catch{}
		}
		return Total;
	}
	void Detail_Del(Object sender, DataGridCommandEventArgs e){
		string Dstring;
		string Nowid=Detail.DataKeys[e.Item.ItemIndex].ToString();
		Dstring = "Delete From OrderDetail where ID =" + Nowid;
		try{
			SqlCommand UpStaff=new SqlCommand(Dstring,Source);
			UpStaff.Connection.Open();
			UpStaff.ExecuteNonQuery();//执行保存
			UpStaff.Connection.Close();
			DetailBind(true);
			BeforeTaxCount();
		}
		catch(Exception p){
			errMsg.Text="Delete fail" + p.ToString();
		}
	}
	void BeforeTaxCount(){
		DataSet NowDS = (DataSet)Session["NowDO"];
		Label NowID = (Label)Orders.Items[0].FindControl("OrderIDL");
		NowDS.Tables["OrdersN"].Rows[0]["BeforeTaxPrice"]=BeforeTaxTotal();
		Double a = 0;
		if(NowDS.Tables["OrdersN"].Rows[0]["Tax"].ToString()=="True"){
			a=BeforeTaxTotal()*0.05;//计算税金
		}
		Double y=BeforeTaxTotal() + a;//含税价
		string w1="Update Orders Set BeforeTaxPrice =" + BeforeTaxTotal() + ",TotalPrice =" + y;
		w1 += ",TaxPrice =" + a + " where OrderID='" + NowID.Text + "'";
		SqlCommand UpOrder=new SqlCommand(w1,Source);
		UpOrder.Connection.Open();
		UpOrder.ExecuteNonQuery();//执行保存
		UpOrder.Connection.Close();
		NowRecord=Convert.ToInt32(CurrentRecord.Text)-1;
		bindNew(true);
	}
	</script>
<script language="javascript">
<!--
function CalCu(){
var b=0;
var x=0;
var c=0;
b=Order.Orders__ctl1_BeforeTaxPrice.value-c;
if(Order.TaxClient.checked==true){
	x=b*0.05;
	Order.Orders__ctl1_TaxPrice.value=x;
	Order.Orders__ctl1_TotalPrice.value=b + x;
}else{
	Order.Orders__ctl1_TaxPrice.value=0;
	Order.Orders__ctl1_TotalPrice.value=b;
}
}
// -->
</script>
<link rel=stylesheet type="text/css" href="Style.css">
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server" id="Order">
<asp:LinkButton ID="First" Tooltip="First" OnCommand="ChangeRecord" Runat="server" Text="首条" CommandArgument="First" cssclass="Tab_08" />
<asp:LinkButton ID="Prev" Tooltip="Prev" OnCommand="ChangeRecord" Runat="server" Text="上条" CommandArgument="Prev"  cssclass="Tab_08"/>
<asp:LinkButton ID="Next" Tooltip="Next" OnCommand="ChangeRecord" Runat="server" Text="下条" CommandArgument="Next"  cssclass="Tab_08"/>
<asp:LinkButton ID="Last" Tooltip="Last" OnCommand="ChangeRecord" Runat="server" Text="末条" CommandArgument="Last"  cssclass="Tab_08"/>
至第<asp:TextBox Columns="3" AutoPostBack="true" ID="ToRecord" OnTextChanged="GotoRecord" Runat="server" />条
<p><ASP:LINKBUTTON ID="AddOrder" runat="server" onclick="AddOrder_Click" Text="Add" />　第<asp:Label ID="CurrentRecord" Tooltip="CurrentRecord" Runat="server" />条，共<asp:Label ID="AllRecords" Tooltip="AllRecords" Runat="server" />条　　
<font color="#FF0000"><b>目前状态：<asp:label id="Status" Tooltip="status" runat="server" /></b></font>
<font color="#FF0000"><b><asp:label id="errMsg" runat="server" /></b></font><br>
<asp:DataList id="Orders" runat="server"
	BorderColor="black"	BorderWidth="1"
	GridLines="Both"	CellPadding="3"
	CellSpacing="0" 	Font-Name="Verdana"
	Font-Size="11pt" 	RepeatColumns="1"
	Width="500px"		HeaderStyle-BackColor="#C0FFFF"
	AlternatingItemStyle-BackColor="Gainsboro"
	EditItemStyle-BackColor="#FFFFC0"
itemStyle-width="450"
	OnEditCommand="Orders_Edit"
	OnUpdateCommand="Orders_Update"
	OnCancelCommand="Orders_Cancel"
            >
	<HeaderTemplate>
                订单基本资料
	</HeaderTemplate>
	<ItemTemplate>
		<asp:LinkButton id="button1" runat="server" Text="Edit" CommandName="edit" /><br>
<table><tr><td width="55%">
		订单编号：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' ID="OrderIDL" runat="server" /><br>
</td><td>
		下单日期：<%# DataBinder.Eval(Container.DataItem,"OrderDate","{0:yyyy/MM/dd}")  %><br>
</td></tr><tr><td>
		客户：<%# DataBinder.Eval(Container.DataItem,"CusName")  %>
</td></tr><tr><td>
		未税价：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"BeforeTaxPrice","{0:#,###}") %>' id="BeforeTaxPriceV" runat="server" cssclass="Tab_07"/>
		含税否：<asp:checkbox Checked='<%# DataBinder.Eval(Container.DataItem,"Tax") %>' runat="server" Enabled="false"/>
</td><td>
		税金：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"TaxPrice","{0:#,###}") %>' id="TaxPrice" Runat="server" cssclass="Tab_07"/>
		含税价：<asp:Label Text='<%# DataBinder.Eval(Container.DataItem,"TotalPrice","{0:#,###}") %>' id="TotalPrice" Runat="server" cssclass="Tab_07"/>
</td></tr></table>
	</ItemTemplate>
	<EditItemTemplate>
		<asp:LinkButton id="button2" runat="server" Text="Save" CommandName="Update" />
		<asp:LinkButton id="button3" runat="server" Text="Cancel" CommandName="Cancel" /><br>
<table><tr><td width="50%">
		订单编号：<asp:Label id="OrderIDV" Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' runat="server" />
			<asp:TextBox Text='<%# DataBinder.Eval(Container.DataItem,"OrderID") %>' ID="OrderID" runat="server" />
<br>
</td><td>
		下单日期：<asp:Textbox Text='<%# DataBinder.Eval(Container.DataItem,"OrderDate","{0:yyyy/MM/dd}") %>' Runat="server" ID="OrderDate" width="100" /><br>
</td></tr><tr><td>
		客户：<asp:DropDownlist id="CusID" Runat="server" />
</td></tr><tr><td>
		未税价：<asp:TextBox Enabled="false" ID="BeforeTaxPrice" Text='<%# DataBinder.Eval(Container.DataItem,"BeforeTaxPrice") %>' Runat="server" width="50"/>
含税否：<input id="TaxClient" type="checkbox" Checked='<%# DataBinder.Eval(Container.DataItem,"Tax") %>' onclick="javascript:CalCu()">
</td><td>
		税金：<asp:TextBox text='<%# DataBinder.Eval(Container.DataItem,"TaxPrice","{0:#,###}") %>' ID="TaxPrice" Runat="server" width="35"/>
		含税价：<asp:TextBox Enabled="false" Text='<%# DataBinder.Eval(Container.DataItem,"TotalPrice") %>' ID="TotalPrice" Runat="server" width="55"/>
</td></tr></table>
	</EditItemTemplate>
</asp:DataList>
<p>明细资料<br>
<asp:linkbutton id="AddDetail" runat="server" onclick="AddDetail_Click" Text="AddDetail" />　<font color="#FF0000"><b>目前状态：<asp:label id="StatusSub" Tooltip="status" runat="server" /></b></font>
<br>      <ASP:DataGrid id="Detail" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="9pt"
        HeaderStyle-BackColor="LightYellow"
	HeaderStyle-HorizontalAlign="Center"
	AutoGenerateColumns="FALSE"
	SelectedItemStyle-BackColor="#FFFFC0"
	ItemStyle-HorizontalAlign="right"
	EditItemStyle-HorizontalAlign="right"
	DataKeyField="ID"
	OnEditCommand="Detail_Edit"
	OnCancelCommand="Detail_Cancel"
	OnUpdateCommand="Detail_Update"
	OnDeleteCommand="Detail_Del"
	ShowFooter="True"
>
      <Columns>
        <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Save" ItemStyle-Wrap="false"/>
	<asp:ButtonColumn Text="Delete" CommandName="Delete"/>
	<asp:TemplateColumn HeaderText="BookName" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "ProductName") %> id="BNameV"/>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "ProductID") %> id="ProductIDL" visible="false"/>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:DropDownlist runat="server" id="ProductID"/>
		</EditItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="Quantity" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Quantity") %> id="QuantityV"/>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:textBox runat="server" AutoPostBack="true" OnTextChanged="Qu_Changed" Text=<%# DataBinder.Eval(Container.DataItem, "Quantity") %> id="Quantity" width="35"/>
		</EditItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="Price" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "Price") %> id="PriceV"/>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox runat="server" AutoPostBack="true" OnTextChanged="Qu_Changed" Text=<%# DataBinder.Eval(Container.DataItem, "Price") %> id="Price" width="35"/>
		</EditItemTemplate>
	</asp:TemplateColumn>
	<asp:TemplateColumn HeaderText="SubTotal" >
		<ItemTemplate>
			<asp:Label runat="server" Text=<%# DataBinder.Eval(Container.DataItem, "TotalThis","{0:#,###}") %> id="TotalThis"/>
		</ItemTemplate>
         <FooterTemplate>
             SubTotal:<asp:Label ID="NowSubTotal" Text="<%# BeforeTaxTotal() %>" Runat="server" />
         </FooterTemplate>
	</asp:TemplateColumn>
	</Columns>
</ASP:DataGrid>
</form>