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

252 строки
9.9 KiB

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