版博士V2.0程序
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

231 linhas
9.5 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using Models;
  3. using Newtonsoft.Json;
  4. using OpenCvSharp.Flann;
  5. using SqlSugar;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Data;
  10. using System.Drawing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows.Forms;
  15. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  16. namespace MaiMuAOI.SysUI.ProductAndStep
  17. {
  18. public partial class ProductClassesFrm : Form
  19. {
  20. Service.ClassesService service = new Service.ClassesService();
  21. List<Classes> lstClasses = new List<Classes>();
  22. ValType valType;
  23. private int Tag;
  24. private bool saveExit = false;
  25. public ProductClassesFrm(int tag, string subject = "类别设置", ValType _valType = ValType.字符串)
  26. {
  27. InitializeComponent();
  28. UIStyle.SetUIStyle(this);
  29. this.uiTitel1.FatherForm = this;
  30. this.Tag = tag;
  31. this.Text = subject;
  32. valType = _valType;
  33. dataGridView2.AutoGenerateColumns = false;
  34. //显示行号与列宽度自动调整
  35. dataGridView2.RowHeadersVisible = true;
  36. dataGridView2.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;
  37. dataGridView2.RowPostPaint += (sender, e) =>
  38. {
  39. SysMgr.showRowNum_onDataGrid_RowPostPaint(this.dataGridView2, sender, e);
  40. };
  41. }
  42. private void InitDataView()
  43. {
  44. //dataGridView2.Rows.Clear();
  45. var exp1 = Expressionable.Create<Classes>()
  46. .And(m => m.Tag == Tag)
  47. //.AndIF(dateTimePicker2.Checked, it => it.CreateTime < dateTimePicker2.Value)
  48. .ToExpression();//注意 这一句 不能少
  49. lstClasses = service.GetList(exp1).OrderBy(m => m.Order).ToList();
  50. if (lstClasses.Count < 1)//wlq 大坑:这个因为如果绑定数据源【list】是不为null但长度是0的 ,一旦点击新增 后 点击行就会抛出 索引为-1的相关错误。这个微软不知道咋设计的
  51. {
  52. lstClasses.Insert(0, new Classes());
  53. dataGridView2.DataSource = lstClasses;
  54. dataGridView2.DataSource = null;
  55. lstClasses.RemoveAt(0);
  56. }
  57. dataGridView2.DataSource = lstClasses;// new BindingSource(lstClasses, null);
  58. //for(int i=0;i<lstClasses.Count;i++)
  59. //{
  60. // lstClasses[i].Order = i;
  61. // dataGridView2.Rows.Add(lstClasses[i].Id, lstClasses[i].Order, lstClasses[i].Name, lstClasses[i].Name);
  62. //}
  63. }
  64. private void ProductClassesFrm_Load(object sender, EventArgs e)
  65. {
  66. InitDataView();
  67. }
  68. private void tsbtnSave_Click(object sender, EventArgs e)
  69. {
  70. try
  71. {
  72. if (lstClasses.Where(m => string.IsNullOrWhiteSpace(m.Name)).Count() > 0)
  73. throw new Exception("请填写名称!");
  74. bool result;
  75. var lstInsert = lstClasses.Where(m => m.Id == 0).ToList();
  76. if (lstInsert.Count > 0)
  77. {
  78. result = service.InsertRange(lstInsert);
  79. if (!result)
  80. throw new Exception("保存失败!");
  81. }
  82. var lstUpdate = lstClasses.Where(m => m.Id > 0).ToList();
  83. if (lstUpdate.Count > 0)
  84. {
  85. result = service.UpdateRange(lstUpdate);
  86. if (!result)
  87. throw new Exception("保存失败!");
  88. }
  89. SysMgr.Instance.WriteSysEditLog("产品类别修改", JsonConvert.SerializeObject(lstUpdate));
  90. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  91. saveExit = true;
  92. this.DialogResult = DialogResult.OK;
  93. this.Close();
  94. }
  95. catch (Exception ex)
  96. {
  97. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  98. }
  99. }
  100. private void tsbtnExit_Click(object sender, EventArgs e)
  101. {
  102. this.Close();
  103. }
  104. private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
  105. {
  106. int Index = this.dataGridView2.CurrentRow.Index;//获取当前选中行的索引
  107. if (Index < this.dataGridView2.Rows.Count && this.dataGridView2.CurrentCell.Value.ToString() == "设置")
  108. {
  109. }
  110. }
  111. private void dataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  112. {
  113. try
  114. {
  115. var id = (int)dataGridView2.Rows[e.RowIndex].Cells["colId"].Value;
  116. var order = (int)dataGridView2.Rows[e.RowIndex].Cells["colOrder"].Value;
  117. var preValue = dataGridView2.Rows[e.RowIndex].Cells["colPreName"].Value.ToString();
  118. if (dataGridView2.Rows[e.RowIndex].Cells["colName"].Value == null || dataGridView2.Rows[e.RowIndex].Cells["colName"].Value.ToString() == "")
  119. {
  120. dataGridView2.Rows[e.RowIndex].Cells["colName"].Value = preValue;
  121. return;
  122. }
  123. var value = dataGridView2.Rows[e.RowIndex].Cells["colName"].Value.ToString();
  124. var count = lstClasses.Where(m => m.Id != id && m.Name == value.ToString()).Count();
  125. if (count > 0)
  126. {
  127. if (!string.IsNullOrWhiteSpace(preValue))
  128. dataGridView2.Rows[e.RowIndex].Cells["colName"].Value = preValue;
  129. else
  130. dataGridView2.Rows[e.RowIndex].Cells["colName"].Value = "";
  131. throw new Exception("已存在相同名称!");
  132. }
  133. //
  134. if (id == 0)
  135. {
  136. if (string.IsNullOrWhiteSpace(value))
  137. return;
  138. var m = lstClasses[lstClasses.Count - 1];
  139. lstClasses[lstClasses.Count - 1] = service.InsertReturnEntity(lstClasses[lstClasses.Count - 1]);
  140. //if (!result)
  141. // throw new Exception("保存失败!");
  142. }
  143. else
  144. {
  145. if (value.Equals(preValue))//未修改
  146. return;
  147. bool result = service.InsertOrUpdate(lstClasses[e.RowIndex]);
  148. if (!result)
  149. throw new Exception("保存失败!");
  150. }
  151. dataGridView2.Rows[e.RowIndex].Cells["colPreName"].Value = dataGridView2.Rows[e.RowIndex].Cells["colName"].Value;
  152. }
  153. catch (Exception ex)
  154. {
  155. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  156. }
  157. }
  158. private void tsbtnNew_Click(object sender, EventArgs e)
  159. {
  160. if (dataGridView2.EndEdit())
  161. {
  162. //int index=dataGridView2.Rows.Add();
  163. if (lstClasses.Count < 1 || lstClasses.Where(m => m.Id == 0).Count() < 1)
  164. {
  165. var model = new Classes()
  166. {
  167. Tag = this.Tag,
  168. Name = "",
  169. Order = lstClasses.Count,
  170. CreateUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  171. ModifyUserCode = SysMgr.Instance.UserMgr.LoginUser.Code,
  172. };
  173. lstClasses.Add(model);
  174. dataGridView2.DataSource = null;
  175. dataGridView2.DataSource = lstClasses;
  176. dataGridView2.Rows[dataGridView2.Rows.Count - 1].Selected = true;
  177. dataGridView2.CurrentCell = dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells["colName"];
  178. this.dataGridView2.BeginEdit(true);
  179. }
  180. }
  181. }
  182. private void tsbtnDel_Click(object sender, EventArgs e)
  183. {
  184. try
  185. {
  186. if (!dataGridView2.EndEdit()) return;
  187. if (dataGridView2.SelectedRows.Count < 1)
  188. return;
  189. int index = dataGridView2.SelectedRows[0].Index;
  190. if (lstClasses[index].Id > 0)
  191. {
  192. if (!service.DeleteById(lstClasses[index].Id))
  193. throw new Exception("删除失败,此项可能已被使用!");
  194. }
  195. this.InitDataView();
  196. }
  197. catch (Exception ex)
  198. {
  199. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  200. }
  201. }
  202. private void dataGridView2_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  203. {
  204. //var list = ((BindingSource)dataGridView2.DataSource).DataSource as List<Order>;
  205. for (int i = 0; i < dataGridView2.Rows.Count; i++)
  206. {
  207. dataGridView2.Rows[i].Cells["colPreName"].Value = dataGridView2.Rows[i].Cells["colName"].Value;
  208. }
  209. }
  210. }
  211. }