📄 采购.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BookStore.Forms
{
public partial class StockTable : Form
{
private DataTable newTable;
//保存从"商品信息维护"窗体中选择后传过来的数据,它们将被放入DataGrid的相应表格中
public static string[] inputDataGridArray = new string[] { null, null, null };
//保存计算后的金额,税后金额,税额等信息
private static decimal[] ouputSum = new decimal[] { 0, 0, 0 };
//保存从"供货商信息维护"窗体中选择后传过来的数据,它们将被赋值给供货商号和名称的控件Text属性
//确定要赋的值为文本,故用空字符初始化
public static string[] supplierArray = new string[] { "", "" };
//保存从“员工信息维护”窗体中传过来的数据
public static string[] employeeArray = new string[] { "", "" };
private DateTime gridMouseDownTime; //记录点击DataGrid时的时间
private int i = 0;
public StockTable()
{
InitializeComponent();
DataGridStateControl();
}
//——设置DataGrid
private void DataGridStateControl()
{
newTable = new DataTable();
newTable.Columns.Add("货号(双击)", typeof(decimal));
newTable.Columns.Add("名称",typeof(string));
newTable.Columns.Add("数量", typeof(decimal));
newTable.Columns.Add("单价", typeof(decimal));
newTable.Columns.Add("金额", typeof(decimal),"数量*单价");
newTable.Columns.Add("税率", typeof(decimal));
newTable.Columns.Add("税后金额", typeof(decimal),"金额/1.17");
newTable.Columns.Add("税额",typeof(decimal),"金额-税后金额");
this.dg_Stock.DataSource = newTable;
newTable.Rows.Add(newTable.NewRow());
DataGridTableStyle ts = new DataGridTableStyle();
DataGridTextBoxColumn aColumnText ;
ts.AllowSorting = false;
ts.AlternatingBackColor = Color.LightGray;
ts.MappingName = newTable.TableName;
int numCols = newTable.Columns.Count;
for (int i = 0; i < numCols; i++)
{
aColumnText=new DataGridTextBoxColumn();
if (i != 0 &&i != 2 && i != 3)
{
aColumnText.ReadOnly = true;
}
if (i == 0)//当单击鼠标时相应
{
aColumnText.TextBox.MouseDown += new MouseEventHandler(TextBoxMouseDownHandler);
}
aColumnText.MappingName = newTable.Columns[i].ColumnName;
aColumnText.HeaderText = newTable.Columns[i].ColumnName;
aColumnText.NullText = "";
ts.GridColumnStyles.Add(aColumnText);
}
this.dg_Stock.TableStyles.Add(ts);
}
//—— 处理DataGrid中的双击事件
private void TextBoxMouseDownHandler(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && e.Clicks == 2 || DateTime.Now < gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
{
if (dg_Stock.CurrentCell.ColumnNumber == 0)
{
i = this.dg_Stock.CurrentCell.RowNumber;
ProductsInfo newfrm = new ProductsInfo(false);
newfrm.DataGridReadOnly();
newfrm.ShowDialog();
SetProduct();
SendKeys.Send("{Tab}");
SendKeys.Send("{Tab}");
}
}
}
//——处理手工输入产品信息
private void SelectProInfo()
{
int CurrentRowNum = this.dg_Stock.CurrentCell.RowNumber;
//判断修改的是不商品编号信息,是的话去连接数据库,查询商品信息
if (this.dg_Stock.CurrentCell.ColumnNumber == 0)
{
if (this.dg_Stock[CurrentRowNum, 0].ToString() != "")
{
int strSeachWord = Convert.ToInt32(this.dg_Stock[CurrentRowNum, 0].ToString().Trim());
string select = "select ProductName,UnitPrice from Products where ProductID='" + strSeachWord + "'";
DataTable tempTable = DataAccess.Narnu.GetDataTableBySql(select);
inputDataGridArray[0] = this.dg_Stock[CurrentRowNum, 0].ToString();
inputDataGridArray[1] =tempTable.Rows[0][0].ToString();
inputDataGridArray[2] =tempTable.Rows[0][1].ToString();
SetProduct();
}
else
{
inputDataGridArray[0] = "0";
inputDataGridArray[1] = "";
inputDataGridArray[2] = "0";
SetProduct();
}
}
}
//——计算金额,税后价格//inputDataGridArray[2]中保存的是单价,dg_Stock的第三列式数量,第六列是税率
private void Calculate()
{
/*
int intCurrentRowNumber = this.dg_Stock.CurrentCell.RowNumber;
string strWareCount = this.dg_Stock[intCurrentRowNumber,1].ToString();//数量
string strWarePrice = this.dg_Stock[intCurrentRowNumber,2].ToString();
string strTotalCounter = this.dg_Stock[intCurrentRowNumber,4].ToString();
//当销售价和销售数量不为空时则将当前光标移到下一行的开始处,以便DataTable依据表达式自动计算DataColumn的值
if (strWareCount != "" && strWarePrice != "" && strTotalCounter=="")
{
this.dg_Stock.CurrentCell = new DataGridCell(intCurrentRowNumber+1,0);
}
*/
//为底部三个文本框计算总金额
decimal intTotalCount = 0;
decimal intNoTax = 0;
decimal intTax = 0;
//首先判断列是否为空
object obj = newTable.Compute("SUM (金额)","");
if (obj.GetType().ToString() != "System.DBNull")
{
intTotalCount = (decimal)this.newTable.Compute("SUM (金额)","");
intNoTax = (decimal)this.newTable.Compute("SUM (税后金额)","");
intTax = (decimal)this.newTable.Compute("SUM (税额)","");
}
this.txt_TotalCount.Text = intTotalCount.ToString("N");
this.txt_NoTax.Text = intNoTax.ToString("N");
this.txt_Tax.Text = intTax.ToString("N");
}
//——把取回的商品信息填入DataGrid
private void SetProduct()
{
int CurrentRowNum=i;
this.dg_Stock[CurrentRowNum, 0] = inputDataGridArray[0];
this.dg_Stock[CurrentRowNum, 1] = inputDataGridArray[1];
this.dg_Stock[CurrentRowNum, 3] = inputDataGridArray[2];
this.dg_Stock[CurrentRowNum, 5] = 17;
}
private void SetEmployee()
{
tbEmployee.Text=employeeArray[0].ToString().Trim();
tbEmployee.Enabled=false;
tb_Info.Text = "采购员:"+employeeArray[1].ToString().Trim();
}
private void SetSupplier()
{
tbSupplier.Text = supplierArray[0].ToString().Trim();
tbSupplier.Enabled = false;
tb_Info2.Text = "供应商:" + supplierArray[1].ToString().Trim();
}
private void btnEmployee_Click(object sender, EventArgs e)
{
EmployInfoManage newFrm = new EmployInfoManage(false);
newFrm.setDataGridReadOnly();
newFrm.ShowDialog();
SetEmployee();
}
private void btnSupplier_Click(object sender, EventArgs e)
{
SuppliersInfo newFrm = new SuppliersInfo();
newFrm.DataGridReadOnly();
newFrm.ShowDialog();
SetSupplier();
}
private void button1_Click(object sender, EventArgs e)
{
ProductsInfo newfrm = new ProductsInfo(false);
newfrm.DataGridReadOnly();
newfrm.ShowDialog();
SetProduct();
SendKeys.Send("{Tab}");
SendKeys.Send("{Tab}");
}
private void button2_Click(object sender, EventArgs e)
{
newProduct newFrm = new newProduct();
newFrm.ShowDialog();
}
private void btnTotal_Click(object sender, EventArgs e)
{
Calculate();
}
//点击工具栏按钮时判断
private void toolBar1_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
if (e.Button.ToolTipText == "保存")
{
if (StockSave())
{
MessageBox.Show("采购单保存成功");
}
}
else if (e.Button.ToolTipText == "删除")
{
try
{
if (newTable.Rows.Count > 0)
{
this.newTable.Rows.RemoveAt(this.dg_Stock.CurrentCell.RowNumber);
this.Calculate();
}
if (newTable.Rows.Count - 1 <= 0)
newTable.Rows.Add(newTable.NewRow());
}
catch
{
return;
}
}
else if (e.Button.ToolTipText == "打印")
{
}
else if (e.Button.ToolTipText == "退出")
{
this.Close();
}
}
//点击保存按钮
//订货单的保存要在两个表中插入 一个保存表头 包括供应商 采购员 采购时间 制单人等信息
//每个表头对应多个表身 产品信息
private bool StockSave()
{
bool result = false;
//取出表单的最大号
string SelectMaxStockID="select max(StockID) from Stocks";
//插入表头
int StockID = 0;
object strMaxID = DataAccess.Narnu.GetObjectBySqlString(SelectMaxStockID);
if (strMaxID.ToString() =="" )
{
StockID = 1;
}
else
{
StockID = int.Parse(strMaxID.ToString());
StockID++;
}
int intEmployeeID = int.Parse(this.tbEmployee.Text.Trim());
int intSupplierID = int.Parse(this.tbSupplier.Text.Trim());
string StockTime = DateTime.Now.ToString();
decimal decTotalPrice = decimal.Parse(this.txt_TotalCount.Text.Trim());
decimal decTotalTax = decimal.Parse(this.txt_Tax.Text.Trim());
decimal decNoTax = decimal.Parse(this.txt_NoTax.Text.Trim());
string ZDR="红猪";
string SetValue = "(" + StockID + "," + intSupplierID + "," + intEmployeeID + "," + decTotalPrice + ",'" + ZDR + "','" +
StockTime + "'," + decTotalTax + "," + decNoTax + ")";
string insertStock = "insert into Stocks values " + SetValue;
try
{
DataAccess.Narnu.DoSqlString(insertStock);
result = true;
}
catch
{
result = false;
MessageBox.Show("表头插入出现错误!");
}
//插入表身
SelectMaxStockID = "Select Max(DetailID) from StockDetails";
int DetailStockID = 0;
object strMaxDetailID = DataAccess.Narnu.GetObjectBySqlString(SelectMaxStockID);
if (strMaxDetailID.ToString() == "")
{
DetailStockID = 1;
}
else
{
DetailStockID = int.Parse(strMaxDetailID.ToString());
DetailStockID++;
}
for (int i = 0; i < newTable.Rows.Count; i++)
{
int intProductID = int.Parse(this.dg_Stock[i, 0].ToString().Trim());
decimal decUnitPrice = decimal.Parse(this.dg_Stock[i, 4].ToString().Trim());
int intUnits = int.Parse(this.dg_Stock[i, 2].ToString().Trim());
decimal decTotalDetailPrice = decimal.Parse(this.dg_Stock[i, 4].ToString().Trim());
string SetDetailValue = "(" + DetailStockID + "," + StockID + "," + intProductID + "," + decUnitPrice + "," +
intUnits + "," + decTotalDetailPrice + ")";
string insertDetail = "insert into StockDetails values " + SetDetailValue;
try
{
DataAccess.Narnu.DoSqlString(insertDetail);
result = true;
}
catch
{
result = false;
MessageBox.Show("表身插入出现错误!");
}
DetailStockID++;
}
//在商品表中更新数量
for (int i = 0; i < newTable.Rows.Count; i++)
{
int intProductID = int.Parse(this.dg_Stock[i, 0].ToString().Trim());
string select = "select Units from Products where ProductID=" + intProductID;
int intUnits = int.Parse(this.dg_Stock[i, 2].ToString().Trim());
intUnits += int.Parse(DataAccess.Narnu.GetObjectBySqlString(select).ToString());
string update = "update products set Units=" + intUnits + "where ProductID=" + intProductID;
try
{
DataAccess.Narnu.DoSqlString(update);
result = true;
}
catch
{
MessageBox.Show("更新商品数量时出现错误!");
result = false;
}
}
return result;
}
//打印采购单
private bool StockPrint()
{
bool result = false;
return result;
}
//点击删除按钮
private bool StockDel()
{
bool result = false;
return result;
}
private void StockTable_Load(object sender, EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -