/// <summary> /// 数据区点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { string action = dataGridView.Columns[e.ColumnIndex].Name; //操作类型 var cells = dataGridView.Rows[e.RowIndex].Cells; //当前行所有列的数据 if (action.Equals("_Update")) { if (MessageBox.Show("确定修改这行数据吗?", "修改提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { Tb_YZSY yZSY = new Tb_YZSY() { Id = Convert.ToInt32(cells["_Id"].Value), MDirection = Convert.ToString(cells["_MDirection"].Value), WDirection = Convert.ToString(cells["_WDirection"].Value), DescribeZ = Convert.ToString(cells["_DescribeZ"].Value), CDirection = Convert.ToString(cells["_CDirection"].Value), DescribeF = Convert.ToString(cells["_DescribeF"].Value), }; List <Tb_YZSY> yZSies = new List <Tb_YZSY>(); yZSies.Add(yZSY); DbBaseResult baseResult = YzsyManament.GetInstance().UpdateYzsys(yZSies) as DbBaseResult; if (baseResult == null) { MessageBox.Show("修改失败!"); } else { MessageBox.Show($"修改结果:成功{baseResult.SuccessNum}个,总共{baseResult.TotalNum}个!"); RefreshYzsyList(Convert.ToInt32(this.comboBox4.SelectedItem.ToString()), currentIndex); } } } if (action.Equals("_Delete")) { if (MessageBox.Show("确定删除这行数据吗?", "删除提示", MessageBoxButtons.OKCancel) == DialogResult.OK) { var _id = cells["_Id"].Value; string[] idArray = new string[] { $"{_id}" }; DbBaseResult baseResult = YzsyManament.GetInstance().DeleteYzsys(idArray) as DbBaseResult; if (baseResult == null) { MessageBox.Show("删除失败!"); } else { MessageBox.Show($"删除结果:成功{baseResult.SuccessNum}个,总共{baseResult.TotalNum}个!"); RefreshYzsyList(Convert.ToInt32(this.comboBox4.SelectedItem.ToString()), currentIndex); } } } }
/// <summary> /// 修改数据 /// </summary> /// <param name="updateStr"></param> /// <returns></returns> public object UpdateYzsys(List <Tb_YZSY> updates) { using (CompassDbContext dbContext = new CompassDbContext()) { try { DbBaseResult baseResult = new DbBaseResult() { TotalNum = updates.Count, SuccessNum = updates.Count }; foreach (var item in updates) { var obj = dbContext.Yzsys.Where(d => d.Id.Equals(item.Id)).FirstOrDefault(); if (obj == null) { continue; } obj.MDirection = item.MDirection; obj.WDirection = item.WDirection; obj.DescribeZ = item.DescribeZ; obj.CDirection = item.CDirection; obj.DescribeF = item.DescribeF; var n = dbContext.SaveChanges(); if (n < 0) { baseResult.SuccessNum--; } } return(baseResult); } catch (Exception ex) { MessageBox.Show($"修改YZSY失败,{ex.ToString()}"); } } return(null); }
/// <summary> /// 删除数据 /// </summary> /// <param name="idArray"></param> /// <returns></returns> public object DeleteYzsys(string[] idArray) { using (CompassDbContext dbContext = new CompassDbContext()) { try { DbBaseResult baseResult = new DbBaseResult() { TotalNum = idArray.Length, SuccessNum = idArray.Length }; for (int i = 0; i < idArray.Length; i++) { int tempid = int.Parse(idArray[i]); var obj = dbContext.Yzsys.Where(r => r.Id.Equals(tempid)).FirstOrDefault(); if (obj == null) { continue; } dbContext.Yzsys.Remove(obj); var n = dbContext.SaveChanges(); if (n < 0) { baseResult.SuccessNum--; } } return(baseResult); } catch (Exception ex) { MessageBox.Show($"删除YZSY失败,{ex.ToString()}"); } } return(null); }