版博士V2.0程序
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

228 рядки
9.3 KiB

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