📄 frmeditdetail.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace OneToMany
{
/// <summary>
/// frmEditDetail 的摘要说明。
/// </summary>
public class frmEditDetail : System.Windows.Forms.Form
{
internal System.Windows.Forms.ComboBox cboProduct;
internal System.Windows.Forms.Button btnOK;
internal System.Windows.Forms.Button btnCancel;
internal System.Windows.Forms.TextBox txtItemTotal;
internal System.Windows.Forms.Label lblItemTotal;
internal System.Windows.Forms.TextBox txtQuantity;
internal System.Windows.Forms.Label lblQuantity;
internal System.Windows.Forms.TextBox txtUnitPrice;
internal System.Windows.Forms.Label lblUnitPrice;
internal System.Windows.Forms.Label lblProductID;
internal System.Windows.Forms.TextBox txtOrderID;
internal System.Windows.Forms.Label lblOrderID;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmEditDetail()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cboProduct = new System.Windows.Forms.ComboBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.txtItemTotal = new System.Windows.Forms.TextBox();
this.lblItemTotal = new System.Windows.Forms.Label();
this.txtQuantity = new System.Windows.Forms.TextBox();
this.lblQuantity = new System.Windows.Forms.Label();
this.txtUnitPrice = new System.Windows.Forms.TextBox();
this.lblUnitPrice = new System.Windows.Forms.Label();
this.lblProductID = new System.Windows.Forms.Label();
this.txtOrderID = new System.Windows.Forms.TextBox();
this.lblOrderID = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cboProduct
//
this.cboProduct.DisplayMember = "ProductName";
this.cboProduct.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboProduct.Location = new System.Drawing.Point(120, 48);
this.cboProduct.Name = "cboProduct";
this.cboProduct.Size = new System.Drawing.Size(280, 23);
this.cboProduct.TabIndex = 15;
this.cboProduct.ValueMember = "ProductName";
this.cboProduct.SelectionChangeCommitted += new System.EventHandler(this.cboProduct_SelectionChangeCommitted);
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
this.btnOK.Location = new System.Drawing.Point(208, 184);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(96, 32);
this.btnOK.TabIndex = 23;
this.btnOK.Text = "确定(&O)";
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnCancel.Location = new System.Drawing.Point(304, 184);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(96, 32);
this.btnCancel.TabIndex = 22;
this.btnCancel.Text = "取消(&C)";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// txtItemTotal
//
this.txtItemTotal.BackColor = System.Drawing.Color.White;
this.txtItemTotal.Location = new System.Drawing.Point(120, 144);
this.txtItemTotal.Name = "txtItemTotal";
this.txtItemTotal.ReadOnly = true;
this.txtItemTotal.Size = new System.Drawing.Size(280, 24);
this.txtItemTotal.TabIndex = 21;
this.txtItemTotal.Text = "";
this.txtItemTotal.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// lblItemTotal
//
this.lblItemTotal.Location = new System.Drawing.Point(32, 147);
this.lblItemTotal.Name = "lblItemTotal";
this.lblItemTotal.Size = new System.Drawing.Size(92, 17);
this.lblItemTotal.TabIndex = 20;
this.lblItemTotal.Text = "金额:";
this.lblItemTotal.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtQuantity
//
this.txtQuantity.BackColor = System.Drawing.Color.White;
this.txtQuantity.Location = new System.Drawing.Point(120, 112);
this.txtQuantity.Name = "txtQuantity";
this.txtQuantity.Size = new System.Drawing.Size(280, 24);
this.txtQuantity.TabIndex = 19;
this.txtQuantity.Text = "";
this.txtQuantity.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtQuantity.Validating += new System.ComponentModel.CancelEventHandler(this.txtQuantity_Validating);
//
// lblQuantity
//
this.lblQuantity.Location = new System.Drawing.Point(32, 115);
this.lblQuantity.Name = "lblQuantity";
this.lblQuantity.Size = new System.Drawing.Size(92, 17);
this.lblQuantity.TabIndex = 18;
this.lblQuantity.Text = "数量:";
this.lblQuantity.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lblQuantity.Click += new System.EventHandler(this.lblQuantity_Click);
//
// txtUnitPrice
//
this.txtUnitPrice.BackColor = System.Drawing.Color.White;
this.txtUnitPrice.Location = new System.Drawing.Point(120, 80);
this.txtUnitPrice.Name = "txtUnitPrice";
this.txtUnitPrice.Size = new System.Drawing.Size(280, 24);
this.txtUnitPrice.TabIndex = 17;
this.txtUnitPrice.Text = "";
this.txtUnitPrice.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.txtUnitPrice.Validating += new System.ComponentModel.CancelEventHandler(this.txtUnitPrice_Validating);
//
// lblUnitPrice
//
this.lblUnitPrice.Location = new System.Drawing.Point(32, 83);
this.lblUnitPrice.Name = "lblUnitPrice";
this.lblUnitPrice.Size = new System.Drawing.Size(92, 17);
this.lblUnitPrice.TabIndex = 16;
this.lblUnitPrice.Text = "单价:";
this.lblUnitPrice.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// lblProductID
//
this.lblProductID.Location = new System.Drawing.Point(32, 51);
this.lblProductID.Name = "lblProductID";
this.lblProductID.Size = new System.Drawing.Size(92, 17);
this.lblProductID.TabIndex = 14;
this.lblProductID.Text = "商品名称:";
this.lblProductID.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// txtOrderID
//
this.txtOrderID.BackColor = System.Drawing.Color.White;
this.txtOrderID.Location = new System.Drawing.Point(120, 16);
this.txtOrderID.Name = "txtOrderID";
this.txtOrderID.ReadOnly = true;
this.txtOrderID.Size = new System.Drawing.Size(280, 24);
this.txtOrderID.TabIndex = 13;
this.txtOrderID.Text = "";
this.txtOrderID.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
//
// lblOrderID
//
this.lblOrderID.Location = new System.Drawing.Point(32, 19);
this.lblOrderID.Name = "lblOrderID";
this.lblOrderID.Size = new System.Drawing.Size(92, 17);
this.lblOrderID.TabIndex = 12;
this.lblOrderID.Text = "订单编号:";
this.lblOrderID.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// frmEditDetail
//
this.AcceptButton = this.btnOK;
this.AutoScaleBaseSize = new System.Drawing.Size(8, 17);
this.CancelButton = this.btnCancel;
this.ClientSize = new System.Drawing.Size(424, 237);
this.Controls.Add(this.cboProduct);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.txtItemTotal);
this.Controls.Add(this.txtQuantity);
this.Controls.Add(this.txtUnitPrice);
this.Controls.Add(this.txtOrderID);
this.Controls.Add(this.lblItemTotal);
this.Controls.Add(this.lblQuantity);
this.Controls.Add(this.lblUnitPrice);
this.Controls.Add(this.lblProductID);
this.Controls.Add(this.lblOrderID);
this.Font = new System.Drawing.Font("宋体", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "frmEditDetail";
this.Text = "编辑订单明细";
this.Load += new System.EventHandler(this.frmEditDetail_Load);
this.ResumeLayout(false);
}
#endregion
DSOneToMany dsOneToMany;
DSOneToMany.Order_DetailsRow rowDetail;
DataRowView drvDetail;
DataView vueProducts;
public void EditDetail(DataRowView Detail)
{
drvDetail = Detail;
rowDetail = (DSOneToMany.Order_DetailsRow) drvDetail.Row;
dsOneToMany = (DSOneToMany) rowDetail.Table.DataSet;
vueProducts = dsOneToMany.Products.DefaultView;
vueProducts.Sort = "ProductName";
cboProduct.DataSource = vueProducts;
cboProduct.DisplayMember = "ProductName";
cboProduct.ValueMember = "ProductID";
txtOrderID.Text = rowDetail.OrderID.ToString();
if (rowDetail.RowState == DataRowState.Detached)
{
txtQuantity.Text = "1";
SetUnitPrice((int) cboProduct.SelectedValue);
txtItemTotal.Text = txtUnitPrice.Text;
}
else
{
cboProduct.SelectedValue = rowDetail.ProductID;
txtUnitPrice.Text = rowDetail.UnitPrice.ToString("c");
txtQuantity.Text = rowDetail.Quantity.ToString();
txtItemTotal.Text = rowDetail.ItemTotal.ToString("c");
}
if (this.ShowDialog() == DialogResult.OK)
{
try
{
if (rowDetail.RowState != DataRowState.Detached)
drvDetail.BeginEdit();
drvDetail["ProductID"] = (int) (cboProduct.SelectedValue);
drvDetail["ProductName"] = dsOneToMany.Products.FindByProductID((int) (cboProduct.SelectedValue))["ProductName"];
drvDetail["Quantity"] = Int32.Parse(txtQuantity.Text);
drvDetail["UnitPrice"] = Decimal.Parse(txtUnitPrice.Text, System.Globalization.NumberStyles.Currency, null);
drvDetail.EndEdit();
}
catch(Exception e)
{
MessageBox.Show(e.Message, "更新错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
drvDetail.CancelEdit();
}
}
else if (rowDetail.RowState == DataRowState.Detached)
drvDetail.CancelEdit();
}
private void SetUnitPrice(int ProductID)
{
DSOneToMany.ProductsRow rowProduct;
rowProduct = dsOneToMany.Products.FindByProductID(ProductID);
txtUnitPrice.Text = rowProduct.UnitPrice.ToString("c");
txtUnitPrice.Modified = true;
txtItemTotal.Text = (Int32.Parse(txtQuantity.Text) * Decimal.Parse(txtUnitPrice.Text, System.Globalization.NumberStyles.Currency, null)).ToString("c");
txtItemTotal.Modified = true;
}
private void cboProduct_SelectionChangeCommitted(object sender, System.EventArgs e)
{
SetUnitPrice((int) cboProduct.SelectedValue);
}
private void txtQuantity_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
int intQuantity = Int32.Parse(txtQuantity.Text);
txtQuantity.Text = intQuantity.ToString();
txtItemTotal.Text = (intQuantity * Decimal.Parse(txtUnitPrice.Text, System.Globalization.NumberStyles.Currency, null)).ToString("c");
}
catch (FormatException ex)
{
MessageBox.Show("有效性校验错误 - 数量必须是数值型数据!", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
txtQuantity.Undo();
e.Cancel = true;
}
}
private void txtUnitPrice_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
try
{
Decimal decUnitPrice = Decimal.Parse(txtUnitPrice.Text, System.Globalization.NumberStyles.Currency, null);
txtUnitPrice.Text = decUnitPrice.ToString("c");
txtItemTotal.Text = (Int32.Parse(txtQuantity.Text) * decUnitPrice).ToString("c");
}
catch
{
MessageBox.Show("单价必须是数值型数据!", "校验错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtUnitPrice.Undo();
e.Cancel = true;
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
}
private void lblQuantity_Click(object sender, System.EventArgs e)
{
}
private void frmEditDetail_Load(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -