private void dgvCellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } optrowid = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); if (!String.IsNullOrEmpty(optrowid)) { model = dal.GetModel(Int32.Parse(optrowid)); if (model != null) { Model.tb_proc ml = new Model.tb_proc(); ml = dalt.GetModel(int.Parse(model.p_pid.ToString())); txtname.Text = ml.p_name; txtno.Text = ml.p_no; txttyid.Text = model.p_pid.ToString(); txttime.Text = model.p_time; txtnumold.Text = model.p_numold.ToString(); txtnumnow.Text = model.p_numnow.ToString(); txtuser.Text = model.p_user; txtrek.Text = model.p_remark; } } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Model.tb_pan model) { StringBuilder strSql = new StringBuilder(); StringBuilder strSql1 = new StringBuilder(); StringBuilder strSql2 = new StringBuilder(); if (model.p_pid != null) { strSql1.Append("p_pid,"); strSql2.Append("" + model.p_pid + ","); } if (model.p_time != null) { strSql1.Append("p_time,"); strSql2.Append("'" + model.p_time + "',"); } if (model.p_user != null) { strSql1.Append("p_user,"); strSql2.Append("'" + model.p_user + "',"); } if (model.p_numold != null) { strSql1.Append("p_numold,"); strSql2.Append("" + model.p_numold + ","); } if (model.p_numnow != null) { strSql1.Append("p_numnow,"); strSql2.Append("" + model.p_numnow + ","); } if (model.p_remark != null) { strSql1.Append("p_remark,"); strSql2.Append("'" + model.p_remark + "',"); } strSql.Append("insert into tb_pan("); strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1)); strSql.Append(")"); strSql.Append(" values ("); strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1)); strSql.Append(")"); strSql.Append(";select @@IDENTITY"); object obj = DbSQL.GetSingle(strSql.ToString()); if (obj == null) { return(0); } else { return(Convert.ToInt32(obj)); } }
/// <summary> /// 得到一个对象实体 /// </summary> public Model.tb_pan GetModel(int p_id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 "); strSql.Append(" p_id,p_pid,p_time,p_user,p_numold,p_numnow,p_remark "); strSql.Append(" from tb_pan "); strSql.Append(" where p_id=" + p_id + ""); Model.tb_pan model = new Model.tb_pan(); DataSet ds = DbSQL.Query(strSql.ToString()); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["p_id"] != null && ds.Tables[0].Rows[0]["p_id"].ToString() != "") { model.p_id = int.Parse(ds.Tables[0].Rows[0]["p_id"].ToString()); } if (ds.Tables[0].Rows[0]["p_pid"] != null && ds.Tables[0].Rows[0]["p_pid"].ToString() != "") { model.p_pid = int.Parse(ds.Tables[0].Rows[0]["p_pid"].ToString()); } if (ds.Tables[0].Rows[0]["p_time"] != null && ds.Tables[0].Rows[0]["p_time"].ToString() != "") { model.p_time = ds.Tables[0].Rows[0]["p_time"].ToString(); } if (ds.Tables[0].Rows[0]["p_user"] != null && ds.Tables[0].Rows[0]["p_user"].ToString() != "") { model.p_user = ds.Tables[0].Rows[0]["p_user"].ToString(); } if (ds.Tables[0].Rows[0]["p_numold"] != null && ds.Tables[0].Rows[0]["p_numold"].ToString() != "") { model.p_numold = decimal.Parse(ds.Tables[0].Rows[0]["p_numold"].ToString()); } if (ds.Tables[0].Rows[0]["p_numnow"] != null && ds.Tables[0].Rows[0]["p_numnow"].ToString() != "") { model.p_numnow = int.Parse(ds.Tables[0].Rows[0]["p_numnow"].ToString()); } if (ds.Tables[0].Rows[0]["p_remark"] != null && ds.Tables[0].Rows[0]["p_remark"].ToString() != "") { model.p_remark = ds.Tables[0].Rows[0]["p_remark"].ToString(); } return(model); } else { return(null); } }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.tb_pan model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tb_pan set "); if (model.p_pid != null) { strSql.Append("p_pid=" + model.p_pid + ","); } else { strSql.Append("p_pid= null ,"); } if (model.p_time != null) { strSql.Append("p_time='" + model.p_time + "',"); } else { strSql.Append("p_time= null ,"); } if (model.p_user != null) { strSql.Append("p_user='******',"); } else { strSql.Append("p_user= null ,"); } if (model.p_numold != null) { strSql.Append("p_numold=" + model.p_numold + ","); } else { strSql.Append("p_numold= null ,"); } if (model.p_numnow != null) { strSql.Append("p_numnow=" + model.p_numnow + ","); } else { strSql.Append("p_numnow= null ,"); } if (model.p_remark != null) { strSql.Append("p_remark='" + model.p_remark + "',"); } else { strSql.Append("p_remark= null ,"); } int n = strSql.ToString().LastIndexOf(","); strSql.Remove(n, 1); strSql.Append(" where p_id=" + model.p_id + ""); int rowsAffected = DbSQL.ExecuteSql(strSql.ToString()); if (rowsAffected > 0) { return(true); } else { return(false); } }
private void tbBtnClick(object sender, ToolBarButtonClickEventArgs e) { if (e.Button.ToolTipText == "新增") { rstValue(); setModifyMode(true); optrowid = null; } if (e.Button.ToolTipText == "修改") { if (!String.IsNullOrEmpty(optrowid)) { setModifyMode(true); } else { MessageBox.Show("请选择所要修改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (e.Button.ToolTipText == "删除") { if (!String.IsNullOrEmpty(optrowid)) { DialogResult result = MessageBox.Show("确认删除?", "删除数据", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { dal.Delete(int.Parse(optrowid)); rstValue(); MessageBox.Show("恭喜你,删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); bindData(""); setModifyMode(false); optrowid = null; } } else { MessageBox.Show("请选择所要删除的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (e.Button.ToolTipText == "提交") { if (validateInput()) { model = new Model.tb_pan(); if (!String.IsNullOrEmpty(optrowid)) { model = dal.GetModel(int.Parse(optrowid)); } model.p_pid = int.Parse(this.txttyid.Text); model.p_time = this.txttime.Text; int tempNumold = int.Parse(this.txtnumold.Text); if (tempNumold < 0) { MessageBox.Show("输入数量有误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } model.p_numold = tempNumold; int tempNumnow = int.Parse(this.txtnumnow.Text); if (tempNumnow < 0) { MessageBox.Show("输入数量有误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } model.p_numnow = tempNumnow; model.p_user = txtuser.Text; model.p_remark = this.txtrek.Text; if (String.IsNullOrEmpty(optrowid)) { int i = dal.Add(model); if (i > 0) { BLL.tb_proc dalp = new BLL.tb_proc(); Model.tb_proc molp = new Model.tb_proc(); molp = dalp.GetModel(int.Parse(model.p_pid.ToString())); molp.p_num = model.p_numnow; dalp.Update(molp); MessageBox.Show("恭喜你,新增成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); rstValue(); bindData(""); setModifyMode(false); optrowid = null; } } else { if (dal.Update(model)) { BLL.tb_proc dalp = new BLL.tb_proc(); Model.tb_proc molp = new Model.tb_proc(); molp = dalp.GetModel(int.Parse(model.p_pid.ToString())); molp.p_num = model.p_numnow; dalp.Update(molp); MessageBox.Show("恭喜你,修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); rstValue(); bindData(""); setModifyMode(false); optrowid = null; } } } } if (e.Button.ToolTipText == "取消") { bindData(""); rstValue(); setModifyMode(false); optrowid = null; } if (e.Button.ToolTipText == "退出") { this.Close(); } }