/// <summary> /// 按照类型将DataGridView的内容筛选为DataTable /// </summary> /// <param name="dgv"></param> /// <param name="ppy">所要筛选的属性数组</param> /// <param name="type">类型</param> /// <returns></returns> public static DataTable GetTable(DataGridView dgv, List <string> headers, string type) { DataTable dt = new DataTable(); DataColumn dc; DataRow dr; //过滤用的字符串,比headers多的一列是“单号”,单号是过滤的依据 string[] filter = new string[headers.Count + 1]; //如果表头长度只为1,则直接返回一个空的dataTable if (headers.Count == 1) { return(dt); } //根据属性生成表头 for (int count = 0; count < headers.Count; count++) { filter[count] = headers[count]; dc = new DataColumn(headers[count]); dt.Columns.Add(dc); } //filter最后一列 filter[headers.Count] = "单号"; dc = new DataColumn("单号"); dt.Columns.Add(dc); for (int row = 0; row < dgv.Rows.Count; row++) { if (dgv["资产类别", row].Value.ToString() != type) { continue; } dr = dt.NewRow(); for (int index = 0; index < filter.Length; index++) { for (int column = 0; column < dgv.Columns.Count; column++) { if (dgv.Columns[column].HeaderText == filter[index]) { dr[index] = Convert.ToString(dgv.Rows[row].Cells[column].Value); } } } //总表里是没有数量这一列的,需要自行添加 dr["数量"] = EqMgr.AssetCount(dr["单号"].ToString()).ToString(); dt.Rows.Add(dr); } //根据单号去重 dt = GetDistinctSelf(dt, "单号"); dt.Columns.Remove("单号"); return(dt); }
public static DataTable GetTable(DataTable dtsource, List <string> headers, string type) { DataTable dtInNeed = new DataTable(); DataTable dtThisType = new DataTable(); DataRow dr; //过滤用的字符串,比headers多的一列是“单号”,单号是过滤的依据 string[] filter = new string[headers.Count + 1]; //如果表头长度只为1,则直接返回一个空的dataTable if (headers.Count == 1) { return(dtInNeed); } for (int count = 0; count < headers.Count; count++) { filter[count] = headers[count]; } //filter最后一列 filter[headers.Count] = "单号"; //这一步是为了将dtsource的列名直接赋给dtThisType,缺少这一步的话dtThisType是没有列名的,也不能筛选 dtThisType = dtsource.Clone(); dtThisType.Clear(); for (int index = 0; index < dtsource.Rows.Count; index++) { if (dtsource.Rows[index]["资产类别"].ToString() != type) { continue; } dr = dtsource.Rows[index]; dr["数量"] = EqMgr.AssetCount(dtsource.Rows[index]["单号"].ToString()); dtThisType.ImportRow(dr); } dtInNeed = GetFilterTable(dtThisType, filter); dtInNeed = GetDistinctSelf(dtInNeed, "单号"); dtInNeed.Columns.Remove("单号"); return(dtInNeed); }
//保存修改的信息 private void toolEqUpdate_Click(object sender, EventArgs e) { if (this.checkInput()) { Equipment eq = new Equipment(); eq.EqNo = this.txtEqNo.Text; eq.EqName = this.txtEqName.Text; eq.AssetNo = this.txtAssetNo.Text; eq.EduNo = this.txtEduNo.Text; eq.EqType = this.cbxEqType.Text; eq.Gb = this.txtGB.Text; eq.Usage = this.cbxUsage.Text; eq.Unit = this.cbxUnit.Text; eq.Direction = this.cbxDirection.Text; eq.BuyWay = this.cbxBuyWay.Text; eq.GetWay = this.cbxGetWay.Text; eq.Purchaser = this.cbxPurchaser.Text; eq.Agent = this.txtAgent.Text; eq.Brand = this.txtBrand.Text; eq.Model = this.txtModel.Text; eq.Country = this.txtCountry.Text; eq.Mfrs = this.txtMfrs.Text; eq.ProductNo = this.txtProductNo.Text; eq.Supplier = this.txtSupplier.Text; eq.PriceType = this.cbxPriceType.Text; eq.EqKeeper = this.cbxEqKeeper.Text; eq.Department = DepartMgr.GetIdFromName(this.txtDepartment.Text); eq.Campus = this.cbxCampus.Text; eq.KeepPlace = this.cbxKeepPlace.Text; eq.Cn = this.txtCN.Text; eq.InvNo = this.txtInvNo.Text; eq.Funds = this.cbxFunds.Text; eq.BelongTo = ""; eq.Photo = ""; eq.Remark = this.txtRemark.Text; eq.Photo = this.getPhotoPath(); if (this.dtpGetDate.Checked == true) { eq.GetDate = this.dtpGetDate.Value.ToShortDateString(); } else { eq.GetDate = ""; } if (dtpAddDate.Checked == true) { eq.AddDate = this.dtpAddDate.Value.ToShortDateString(); } else { eq.AddDate = ""; } if (this.dtpBirthday.Checked == true) { eq.Birthday = this.dtpBirthday.Value.ToShortDateString(); } else { eq.Birthday = ""; } if (this.dtpSvcDate.Checked == true) { eq.SvcDate = this.dtpSvcDate.Value.ToShortDateString(); } else { eq.SvcDate = ""; } /*****************************根据资产类别判断所要填的项********************************************/ //资产类别为“土地、房屋及构筑物”时 if (eq.EqType == "土地、房屋及构筑物") { eq.Pr = this.cbxPR.Text; eq.Address = this.txtAddress.Text; eq.CertNature = this.cbxCertNature.Text; eq.Structure = this.cbxStructure.Text; //有产权时以下四项才可以被填写 if (eq.Pr == "有产权") { if (this.txtCertNo.Text.Trim() == "") { untCommon.InfoMsg("请输入权属证号"); return; } else { eq.CertNo = this.txtCertNo.Text; } if (this.dtpIssueDate.Checked == false) { untCommon.InfoMsg("请选择发证日期"); return; } else { eq.IssueDate = this.dtpIssueDate.Value.ToShortDateString(); } if (this.txtCertProve.Text.Trim() == "") { untCommon.InfoMsg("请输入权属证明"); return; } else { eq.CertProve = this.txtCertProve.Text; } try { eq.CertLim = int.Parse(this.txtCertLim.Text); } catch (FormatException) { untCommon.ErrorMsg("权属年限请输入数字。"); return; } } else { eq.CertNo = ""; eq.IssueDate = ""; eq.CertProve = ""; eq.CertLim = 0; } try { eq.Area = double.Parse(this.txtArea.Text); } catch (FormatException) { untCommon.ErrorMsg("建筑/土地面积请输入数字。"); return; } try { eq.TenuArea = double.Parse(this.txtTenuArea.Text); } catch (FormatException) { untCommon.ErrorMsg("自用面积请输入数字。"); return; } try { eq.TenuPrice = double.Parse(this.txtTenuPrice.Text); } catch (FormatException) { untCommon.ErrorMsg("自用价值请输入数字。"); return; } } else { eq.Pr = ""; eq.CertNo = ""; eq.IssueDate = ""; eq.CertProve = ""; eq.Address = ""; eq.CertNature = ""; eq.Structure = ""; eq.Area = 0; eq.TenuArea = 0; eq.TenuPrice = 0; eq.CertLim = 0; } //资产类别为"通用设备(车辆)" if (eq.EqType == "通用设备(车辆)") { eq.CarUse = this.cbxCarUse.Text; eq.CarBP = this.cbxCarBP.Text; eq.LicNo = this.txtLicNo.Text; eq.Dspl = this.txtDSPL.Text; eq.EngNo = this.txtEngNo.Text; eq.Formation = this.cbxFormation.Text; } else { eq.CarUse = ""; eq.CarBP = ""; eq.LicNo = ""; eq.Dspl = ""; eq.EngNo = ""; eq.Formation = ""; } //资产类别为"无形资产" if (eq.EqType == "无形资产") { eq.RegAuz = this.txtRegAuz.Text; eq.PatNo = this.txtPatNo.Text; eq.ApvNo = this.txtApvNo.Text; eq.MgtAgency = this.txtMgtAgency.Text; if (this.dtpRegTime.Checked == true) { eq.RegTime = this.dtpRegTime.Value.ToShortDateString(); } else { eq.RegTime = ""; } } else { eq.RegAuz = ""; eq.RegTime = ""; eq.PatNo = ""; eq.ApvNo = ""; eq.MgtAgency = ""; } //资产类别为"文物和陈列品" if (eq.EqType == "文物和陈列品") { eq.RelicLv = this.cbxRelicLv.Text; } else { eq.RelicLv = ""; } /*************************************判断结束******************************************************/ try { count = int.Parse(this.txtCount.Text); } catch (FormatException) { untCommon.ErrorMsg("数量请输入数字。"); return; } try { eq.Price = double.Parse(this.txtPrice.Text); } catch (FormatException) { untCommon.ErrorMsg("价值请输入数字。"); return; } try { if (this.txtUSDPrice.Text.Trim() != "") { eq.UsdPrice = double.Parse(this.txtUSDPrice.Text); } else { eq.UsdPrice = 0; } } catch (FormatException) { untCommon.ErrorMsg("美金单价请输入数字。"); return; } //判断更新模式 switch (this.mode) { case 0: this.DialogResult = DialogResult.OK; break; //直接修改 case 1: { int error = 0; //0级和1级用户直接更新 if (_power == "0" || _power == "1") { foreach (string field in eqnoList) { eq.EqNo = field; eq.State = "入库"; if (EqMgr.Update(eq)) { // } else { error++; } } if (error == 0) { untCommon.InfoMsg("更新成功"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("操作失败,失败数目为:" + error.ToString()); this.DialogResult = DialogResult.OK; this.Close(); } } else { DataTable Empdt = EmployeeMgr.GetAllName(); foreach (string field in eqnoList) { eq.EqNo = "U" + this.Loginid + DateTime.Now.ToString("yyyyMMddHHmmss") + field; eq.State = "更新待审核"; if (EqMgr.Add(eq)) { // } else { error++; } } if (error == 0) { untCommon.InfoMsg("更新成功,请等待审核"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("操作失败,失败数目为:" + error.ToString()); this.DialogResult = DialogResult.OK; this.Close(); } } break; } //直接批量修改 case 2: { string question = "确定要更改这单数量为: "; question += EqMgr.AssetCount(this.asset).ToString() + " 的资产吗?"; if (untCommon.QuestionMsg(question)) { if (_power == "0" || _power == "1") { eq.State = "入库"; if (EqMgr.UpdateByAsset(eq, asset)) { untCommon.InfoMsg("更新成功"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("更新失败。"); this.DialogResult = DialogResult.OK; this.Close(); } } else { List <string> list = EqMgr.GetEqNoByAssetNo(this.asset); DataTable Empdt = EmployeeMgr.GetAllName(); int error = 0; if (list != null) { foreach (string field in list) { eq.EqNo = "U" + this.name2ID(Empdt, this._user, "name") + DateTime.Now.ToString("yyyyMMddHHmmss") + field; eq.State = "更新待审核"; if (EqMgr.Add(eq)) { } else { error++; } } } if (error == 0) { untCommon.InfoMsg("更新信息提交成功,请等待审核"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("更新信息发生错误\n" + "失败数为: " + error.ToString()); this.DialogResult = DialogResult.OK; this.Close(); } } } break; } //从新增审核处修改 case 3: { eq.State = "新增待审核"; if (EqMgr.UpdateByAsset(eq, asset)) { untCommon.InfoMsg("更新信息提交成功,请等待审核"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("更新失败。"); this.DialogResult = DialogResult.OK; this.Close(); } break; } //从更新审核处修改 case 4: { int error = 0; DataTable Empdt = EmployeeMgr.GetAllName(); foreach (string field in eqnoList) { eq.EqNo = field; eq.State = "更新待审核"; if (EqMgr.Update(eq)) { // } else { error++; } } if (error == 0) { untCommon.InfoMsg("更新成功,请等待审核"); this.DialogResult = DialogResult.OK; this.Close(); } else { untCommon.InfoMsg("操作失败,失败数目为:" + error.ToString()); this.DialogResult = DialogResult.OK; this.Close(); } break; } } } }