该系统一共三个窗体:
1、清单界面:
这个界面主要做了三个事件:
一是点查询的时候按照文本框的单号和返工日期进行模糊查询;
二是点击新增的时候在主窗体添加一个新的页签,如果已经添加过,不再重复添加,并把当前页面切换成新页签。
这个过程中涉及到一个单号自动取号的问题,这里会涉及到单号的锁定和解锁的问题,我是直接在新增的时候就把单号插入到数据库的主表中,如果没有保存,再把这一行记录删除。要注意的地方是:每段语法都需要写try{}catch{},因为如果系统意外退出的话数据库会有垃圾数据。其实这段这样做是不太对的,就是会有不可预估的情况导致产生垃圾数据。正确的锁定单号的方法应该是在缓存中锁定,在中间层,这方面我暂时还没有研究,等后续把这一块的知识逐渐完善起来。但是我目前的做法其实没有解决单据跳号的问题,如果存在多人编辑,并且有人取消编辑,就会出现跳号的问题。
三是点击GridControl的第一列的时候,打开对应行项单据的窗体,查看单据内容。页签名称会显示单号,如果单号已经打开过不重复打开。并跳转页面至该单号的页签。
这块涉及到一个点就是GridControl在绑定数据源之后,怎么再添加一个Button列的问题。这段值得记录一下。(但是目前我还没做出来把多个按钮放在一列的方法,下面的方法是一个按钮一列)
//首先定义一个全局的ButtonEdit列
private RepositoryItemButtonEdit btn_Edit = new RepositoryItemButtonEdit();
//然后定义一个函数设置ButtonEdit列的属性(在这里我的编辑其实没用到,被我注释掉了)
private void SetButtonEdit(RepositoryItemButtonEdit btn, string ImageName)
{
btn.Buttons[0].Kind = ButtonPredefines.Glyph;
btn.TextEditStyle = TextEditStyles.HideTextEditor;//隐藏文字
if (string.IsNullOrEmpty(ImageName)) return;
if (ImageName.Equals("查看"))
btn.Buttons[0].ImageUri = "Find;Size16x16";//图片-查看
else if (ImageName.Equals("编辑"))
btn.Buttons[0].ImageUri = "Edit;Size16x16";//图片-编辑
}
//然后在主函数中调用这个函数以及这个按钮的点击事件
public Form_ReworkListSel(Form_Main fmz)
{
InitializeComponent();
fm = fmz;
SetButtonEdit(btn_Open, "查看");
//SetButtonEdit(btn_Edit, "编辑");
btn_Open.Click += btn_Open_Click;
//btnDelete.Click += BtnClick;
}
//然后把按钮列添加到GridControl中
private void GV_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column.GetTextCaption() == "查看")
{
e.RepositoryItem = btn_Open;
}
else if (e.Column.GetTextCaption() == "编辑")
{
e.RepositoryItem = btn_Edit;
}
}
//下面是按钮的点击事件
private void btn_Open_Click(object sender, EventArgs e)
{
bool bzj = false;
if (fm.tabs.Controls.Count != 0)
{
foreach (XtraTabPage page in fm.tabs.TabPages)
{
if (page.Name =="page_"+GV.GetRowCellValue(GV.FocusedRowHandle, "返工单号"))
{
bzj = true;
fm.tabs.SelectedTabPage = page;
return;
}
}
}
if (bzj == false)
{
XtraTabPage tabFrmLog = new XtraTabPage();
tabFrmLog.Text = "返工单 : " + GV.GetRowCellValue(GV.FocusedRowHandle, "返工单号");
tabFrmLog.Name = "page_" + GV.GetRowCellValue(GV.FocusedRowHandle, "返工单号");
//初始化登录界面
Form_ReworkListOpen Form_ReworkListOpen = new Form_ReworkListOpen(fm);
Form_ReworkListOpen.MdiParent = fm;
Form_ReworkListOpen.Parent = tabFrmLog;
Form_ReworkListOpen.Dock = DockStyle.Fill;
Form_ReworkListOpen.FormBorderStyle = FormBorderStyle.None;
Form_ReworkListOpen.txt_BillNo.Text = GV.GetRowCellValue(GV.FocusedRowHandle, "返工单号").ToString();
Form_ReworkListOpen.txt_BillDate.EditValue = GV.GetRowCellValue(GV.FocusedRowHandle, "返工日期");
sql = "select BillNo 返工单号,CustomerName 客户,OuterBoxCode 外箱码,ProductCode 产品码 , BoxQty 箱数, ProductQty 产品数 from A138_ReworkListDetail where BillNo = '"+ GV.GetRowCellValue(GV.FocusedRowHandle, "返工单号").ToString().Trim()+ "'";
DataTable ds;
if (!DB.AF.execSql(sql, sqlconn, out ds))
{
return;
}
Form_ReworkListOpen.gridControl1.DataSource = ds;
Form_ReworkListOpen.Show();
//子窗体大小设置为选项卡大小
fm.tabs.TabPages.Add(tabFrmLog);
fm.tabs.SelectedTabPage = tabFrmLog;
}
}
2.新增界面:
新增的页面做的事情就比较多,包括一系列值的判断以及操作。值得一提的有几点:一是窗体关闭的时候关闭页签,其实这个方法挺简单的,但是我可能是当时写代码写晕了,然后这个小方法还是想了挺久的,所以记录一下。想想好丢人,一点技术含量都没有,但是我当时我就糊涂了,我就一直想着怎么得到页签的index(手动狗头)
private void btnCancel_Click(object sender, EventArgs e)
{
foreach (XtraTabPage page in fm.tabs.TabPages.ToArray())//遍历得到和关闭的选项卡一样的Text
{
if (page.Text == "返工单新增")
{
fm.tabs.TabPages.Remove(page);
}
}
if (!unlockBillNo())
{
MessageBox.Show("返工单号:" + txt_BillNo.Text + "解锁失败!");
return;
}
}
private bool unlockBillNo()
{
SqlConnection sqlCnt = new SqlConnection(sqlconn);
sqlCnt.Open();
SqlCommand command = new SqlCommand();
command.Connection = sqlCnt;
command.CommandType = CommandType.Text;
command.CommandText = "delete from A138_ReworkListMain where BillNo = @BillNo";
command.Parameters.Add("@BillNo", SqlDbType.VarChar, 12).Value = txt_BillNo.Text.Trim();
command.ExecuteNonQuery();
sqlCnt.Close();
return true;
}
二是要提一下的就是GridControl和DataGridview不太一样的一点是,GridControl在没绑定数据源的时候是不能新增行项的,所以这边用了个虚表,先在虚表中插入字段,然后再把虚表赋值给GridControl的数据源。
private void AddRow()
{
int BQty = 1;
DataRow[] BoxQty = sesstab.Select("外箱码 = '" + txt_OuterBoxCode.Text.Trim() + "' and 返工单号='" + txt_BillNo.Text.Trim() + "' and 客户代码='" + ddlCustomer.EditValue + "'");
if (BoxQty.Length > 0)
{
foreach (DataRow row in BoxQty)
{
BQty = int.Parse(row[4].ToString());
}
}
DataRow[] sumqty = sesstab.Select("外箱码 = '" + txt_OuterBoxCode.Text.Trim() + "' and 返工单号='" + txt_BillNo.Text.Trim() + "' and 客户代码='" + ddlCustomer.EditValue + "'");
int sumPro = 0;
if (sumqty.Length > 0)
{
foreach (DataRow row in sumqty)
{
sumPro += int.Parse(row[5].ToString());
}
}
DataRow[] IsAdd = sesstab.Select("外箱码 = '" + txt_OuterBoxCode.Text.Trim() + "' and 返工单号='" + txt_BillNo.Text.Trim() + "' and 产品码 ='" + txt_ProductCode.Text.Trim() + "' and 客户代码='" + ddlCustomer.EditValue + "'");
if (IsAdd.Length == 0 && sumPro < int.Parse(txt_Quantity.Text.Trim()) * BQty)
{
DataRow dr = sesstab.NewRow();
dr["返工单号"] = txt_BillNo.Text.Trim();
dr["客户"] = ddlCustomer.Text.Trim();
dr["外箱码"] = txt_OuterBoxCode.Text.Trim();
dr["产品码"] = txt_ProductCode.Text.Trim();
dr["箱数"] = BQty;
dr["产品数"] = 1;
dr["删除"] = "删除";
dr["客户代码"] = ddlCustomer.EditValue;
sesstab.Rows.InsertAt(dr, 0);
gridControl1.DataSource = sesstab;
GV.Columns[0].OptionsColumn.AllowEdit = false;
GV.Columns[1].OptionsColumn.AllowEdit = false;
GV.Columns[2].OptionsColumn.AllowEdit = false;
GV.Columns[3].OptionsColumn.AllowEdit = false;
GV.Columns[4].OptionsColumn.AllowEdit = false;
GV.Columns[5].OptionsColumn.AllowEdit = false;
GV.Columns[7].Visible = false;
txt_ProductCode.Text = "";
}
else if (IsAdd.Length > 0 && sumPro < int.Parse(txt_Quantity.Text.Trim()) * BQty)
{
for (int i = 0; i < sesstab.Rows.Count; i++)
{
if (sesstab.Rows[i]["外箱码"].ToString().Trim() == txt_OuterBoxCode.Text.Trim() && sesstab.Rows[i]["产品码"].ToString().Trim() == txt_ProductCode.Text.Trim())
{
sesstab.Rows[i]["产品数"] = int.Parse(sesstab.Rows[i]["产品数"].ToString()) + 1;
txt_ProductCode.Text = "";
}
}
}
if (sumPro == int.Parse(txt_Quantity.Text.Trim()) * BQty - 1)
{
txt_OuterBoxCode.Text = "";
}
if (sumPro == int.Parse(txt_Quantity.Text.Trim()) * BQty)
{
BQty++;
for (int i = 0; i < sesstab.Rows.Count; i++)
{
if (sesstab.Rows[i]["外箱码"].ToString().Trim() == txt_OuterBoxCode.Text.Trim() && sesstab.Rows[i]["客户代码"].ToString().Trim() == ddlCustomer.EditValue.ToString())
{
sesstab.Rows[i]["箱数"] = BQty;
}
}
IsAdd = sesstab.Select("外箱码 = '" + txt_OuterBoxCode.Text.Trim() + "' and 返工单号='" + txt_BillNo.Text.Trim() + "' and 产品码 ='" + txt_ProductCode.Text.Trim() + "' and 客户代码='" + ddlCustomer.EditValue + "'");
if (IsAdd.Length == 0 && sumPro < int.Parse(txt_Quantity.Text.Trim()) * BQty)
{
DataRow dr = sesstab.NewRow();
dr["返工单号"] = txt_BillNo.Text.Trim();
dr["客户"] = ddlCustomer.Text.Trim();
dr["外箱码"] = txt_OuterBoxCode.Text.Trim();
dr["产品码"] = txt_ProductCode.Text.Trim();
dr["箱数"] = BQty;
dr["产品数"] = 1;
dr["删除"] = "删除";
dr["客户代码"] = ddlCustomer.EditValue;
sesstab.Rows.InsertAt(dr, 0);
gridControl1.DataSource = sesstab;
GV.Columns[0].OptionsColumn.AllowEdit = false;
GV.Columns[1].OptionsColumn.AllowEdit = false;
GV.Columns[2].OptionsColumn.AllowEdit = false;
GV.Columns[3].OptionsColumn.AllowEdit = false;
GV.Columns[4].OptionsColumn.AllowEdit = false;
GV.Columns[5].OptionsColumn.AllowEdit = false;
GV.Columns[7].Visible = false;
txt_ProductCode.Text = "";
}
else if (IsAdd.Length > 0 && sumPro < int.Parse(txt_Quantity.Text.Trim()) * BQty)
{
for (int i = 0; i < sesstab.Rows.Count; i++)
{
if (sesstab.Rows[i]["外箱码"].ToString().Trim() == txt_OuterBoxCode.Text.Trim() && sesstab.Rows[i]["产品码"].ToString().Trim() == txt_ProductCode.Text.Trim())
{
sesstab.Rows[i]["产品数"] = int.Parse(sesstab.Rows[i]["产品数"].ToString()) + 1;
txt_ProductCode.Text = "";
}
}
}
if (sumPro == int.Parse(txt_Quantity.Text.Trim()) * BQty - 1)
{
txt_OuterBoxCode.Text = "";
}
}
}
加一段日期转周的代码:
string BCode = txt_OuterBoxCode.Text.Trim();
string date = BCode.Substring(BCode.Length - 10, 10);
DateTime datetime = Convert.ToDateTime(date);
int weekQty = datetime.DayOfYear / 7 + 1;
string weekName;
int day = 0;
weekName = datetime.DayOfWeek.ToString();
switch (weekName)
{
case "Sunday": day = 1; break;
case "Monday": day = 2; break;
case "Tuesday": day = 3; break;
case "Wednesday": day = 4; break;
case "Thursday": day = 5; break;
case "Friday": day = 6; break;
case "Saturday": day = 7; break;
}
string year = date.Substring(3, 1);
string BCheckDate = year.ToString() + weekQty.ToString() + day.ToString();
string PCode = txtBarScanner.Text.Trim();
string PCheckDate = PCode.Substring(13, 4);
if (BCheckDate == PCheckDate)
{
txt_ProductCode.Text = txtBarScanner.Text.Trim();
txtBarScanner.Text = "";
}
else if (BCheckDate != PCheckDate)
{
MessageBox.Show("产品码:" + PCode + "与箱码:" + BCode + "日期不符,请核对后重新扫码");
txtBarScanner.Text = "";
txtBarScanner.Focus();
}
3、查看界面: