JHjgjFrom.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using DevExpress.XtraGrid.Views.Grid;
  12. using DevExpress.XtraGrid;
  13. using Uninpho.DBOperation.Model;
  14. using Uninpho.DBOperation.Operation;
  15. using Uninpho.Tools.components.MTLFF;
  16. using System.IO;
  17. using System.Reflection;
  18. namespace Uninpho.Tools.components
  19. {
  20. public partial class JHjgjFrom : DevExpress.XtraEditors.XtraForm
  21. {
  22. bool gsEdit = false;
  23. bool dsEdit = false;
  24. List<T_jgjdistance_table> GSnew = new List<T_jgjdistance_table>();//存储还没有值的空白行
  25. List<T_jgjdistance_table> DSnew = new List<T_jgjdistance_table>();//存储还没有值的空白行
  26. List<T_jgjdistance_table> GSedit = new List<T_jgjdistance_table>();//已有数据修改过的条目
  27. List<T_jgjdistance_table> DSedit = new List<T_jgjdistance_table>();//已有数据修改过的条目
  28. List<T_jgjdistance_table> tablelist = new List<T_jgjdistance_table>();//全部数据
  29. List<T_jgjdistance_table> GS = new List<T_jgjdistance_table>();//高速 展示数据
  30. List<T_jgjdistance_table> DS = new List<T_jgjdistance_table>();//普速 展示数据
  31. public JHjgjFrom()
  32. {
  33. InitializeComponent();
  34. }
  35. private void bandedGridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
  36. {
  37. DataAnalysis.DACommon.addXuHao(e);
  38. }
  39. private void JHjgjFrom_Load(object sender, EventArgs e)
  40. {
  41. tablelist = DataAnalysisToPG.QueryJGJXGJJ();
  42. GS = tablelist.Where(i => i.Sudutype == 0).ToList();
  43. DS = tablelist.Where(i => i.Sudutype == 1).ToList();
  44. initShow(GSgridControl, GS, GSnew);
  45. initShow(DSgridControl, DS, DSnew);
  46. }
  47. private void initShow(GridControl GC, List<T_jgjdistance_table> data, List<T_jgjdistance_table> datanew)
  48. {
  49. var dataSource = data.Concat(datanew).ToList<T_jgjdistance_table>();
  50. GC.DataSource = dataSource;
  51. }
  52. /// <summary>
  53. /// 删除
  54. /// </summary>
  55. private void DelBtn_Click(object sender, EventArgs e)
  56. {
  57. if (UserInform.userinf.Pow == 0)
  58. {
  59. XtraMessageBox.Show("您无权删除数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  60. }
  61. else
  62. {
  63. if (XtraMessageBox.Show("是否删除所选?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
  64. {
  65. if (DSTabControl.SelectedTabPageIndex == 0)
  66. {
  67. if (!GSgridView.OptionsBehavior.Editable)
  68. {
  69. XtraMessageBox.Show("请先启用编辑模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  70. }
  71. else
  72. {
  73. //高速
  74. delData(GSgridView, GSgridView.GetSelectedRows(), ref GS, ref GSnew);
  75. XtraMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  76. }
  77. }
  78. else
  79. {
  80. if (!GSgridView.OptionsBehavior.Editable)
  81. {
  82. XtraMessageBox.Show("请先启用编辑模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  83. }
  84. else
  85. {
  86. //普速
  87. delData(DSgridView, DSgridView.GetSelectedRows(), ref DS, ref DSnew);
  88. XtraMessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  89. }
  90. }
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// 删除数据
  96. /// </summary>
  97. /// <param name="GV"></param>
  98. /// <param name="row"></param>
  99. private void delData(GridView GV, int[] row,ref List<T_jgjdistance_table> data, ref List<T_jgjdistance_table> datanew)
  100. {
  101. for (int i = row.Length - 1; i >= 0; i--)
  102. {
  103. var va1 = GV.GetRowCellValue(row[i], "Id").ToString();
  104. try
  105. {
  106. var aa = data.Where(it => it.Id == va1).Single();
  107. data.Remove(aa);
  108. }
  109. catch
  110. {
  111. }
  112. try
  113. {
  114. var aa = data.Where(it => it.Id == va1).Single();
  115. datanew.Remove(aa);
  116. }
  117. catch
  118. {
  119. }
  120. DataAnalysisToPG.DelJGJXGJJ(va1);
  121. GV.DeleteRow(row[i]);
  122. }
  123. }
  124. /// <summary>
  125. /// 新增
  126. /// </summary>
  127. private void AddBtn_Click(object sender, EventArgs e)
  128. {
  129. if (DSTabControl.SelectedTabPageIndex == 0)
  130. {
  131. //高速
  132. AddData(GSgridControl, GSgridView, ref GS, ref GSnew, 0);
  133. }
  134. else
  135. {
  136. //普速
  137. AddData(DSgridControl, DSgridView, ref DS, ref DSnew, 1);
  138. }
  139. }
  140. private void AddData(GridControl GC, GridView GV, ref List<T_jgjdistance_table> listshow, ref List<T_jgjdistance_table> list, int type)
  141. {
  142. if (!GV.OptionsBehavior.Editable)
  143. {
  144. XtraMessageBox.Show("请先启用编辑模式!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  145. }
  146. else
  147. {
  148. //1、先在数据库创一个空白的条
  149. T_jgjdistance_table da = new T_jgjdistance_table() { Sudutype = type };
  150. DataAnalysisToPG.InsertJGJXGJJ(da);
  151. //2、修改isnew属性为no
  152. tablelist = DataAnalysisToPG.QueryJGJXGJJ();
  153. T_jgjdistance_table dataa = tablelist.Where(i => i.Isnew == null && i.Sudutype == type).Single();
  154. DataAnalysisToPG.UpdataJGJXGJJ(true, dataa);
  155. //3、更新DataScoure
  156. dataa.Isnew = "no";
  157. list.Add(dataa);
  158. //4、更新显示
  159. initShow(GC, listshow, list);
  160. //5、聚焦最后一行
  161. GV.SelectRow(listshow.Count + list.Count - 1);
  162. GV.FocusedRowHandle = listshow.Count - 1;
  163. }
  164. }
  165. /// <summary>
  166. /// 编辑
  167. /// </summary>
  168. private void EditBtn_Click(object sender, EventArgs e)
  169. {
  170. if (DSTabControl.SelectedTabPageIndex == 0)
  171. {
  172. //高速
  173. EditData(GSgridView, ref gsEdit);
  174. }
  175. else
  176. {
  177. //普速
  178. EditData(DSgridView, ref dsEdit);
  179. }
  180. }
  181. private void EditData(GridView GV, ref bool Editbool)
  182. {
  183. if (!GV.OptionsBehavior.Editable)
  184. {
  185. if (UserInform.userinf.Pow == 0)
  186. {
  187. XtraMessageBox.Show("您没有权限执行编辑操作", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  188. }
  189. else
  190. {
  191. Editbool = true;
  192. GV.OptionsBehavior.Editable = true;
  193. EditBtn.Text = "结束编辑";
  194. XtraMessageBox.Show("已启用编辑!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  195. }
  196. }
  197. else
  198. {
  199. Editbool = false;
  200. GV.OptionsBehavior.Editable = false;
  201. EditBtn.Text = "编辑";
  202. XtraMessageBox.Show("已结束编辑!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  203. }
  204. }
  205. /// <summary>
  206. /// tab切换,btn显示切换
  207. /// </summary>
  208. /// <param name="sender"></param>
  209. /// <param name="e"></param>
  210. private void DSTabControl_SelectedPageChanging(object sender, DevExpress.XtraTab.TabPageChangingEventArgs e)
  211. {
  212. if (e.Page.Text == "高速")
  213. {
  214. selectPageBtn(GSgridView, gsEdit);
  215. }
  216. else
  217. {
  218. selectPageBtn(DSgridView, dsEdit);
  219. }
  220. }
  221. private void selectPageBtn(GridView GV, bool Editbool)
  222. {
  223. if (Editbool)
  224. {
  225. GV.OptionsBehavior.Editable = true;
  226. EditBtn.Text = "结束编辑";
  227. }
  228. else
  229. {
  230. GV.OptionsBehavior.Editable = false;
  231. EditBtn.Text = "编辑";
  232. }
  233. }
  234. private async void GSgridView_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
  235. {
  236. await Task.Run(() =>
  237. {
  238. if (DSTabControl.SelectedTabPageIndex == 0)
  239. {
  240. //高速
  241. CellDataChange(GSgridView, e, ref GSnew, ref GSedit, ref GS, 0);
  242. }
  243. else
  244. {
  245. //普速
  246. CellDataChange(DSgridView, e, ref DSnew, ref DSedit, ref DS, 1);
  247. }
  248. });
  249. }
  250. private void CellDataChange(GridView GV, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e, ref List<T_jgjdistance_table> updatalist, ref List<T_jgjdistance_table> updatalistold, ref List<T_jgjdistance_table> updatalistall, int sudutype)
  251. {
  252. //收集变化的值
  253. string nana = GV.FocusedColumn.FieldName;
  254. string Caption = GV.FocusedColumn.Caption;
  255. string val = GV.GetRowCellValue(e.RowHandle, nana).ToString();
  256. string uuid = GV.GetRowCellValue(e.RowHandle, "Id").ToString();
  257. string typ = GV.GetRowCellValue(e.RowHandle, "Isnew").ToString();
  258. if (typ == "no")
  259. {
  260. //新行
  261. loopProp(ref updatalist, uuid, val, nana);
  262. }
  263. else if (typ == "yes")
  264. {
  265. int isis = 0;
  266. try
  267. {
  268. updatalistold.Where(i => i.Id == uuid).Single();
  269. }
  270. catch
  271. {
  272. isis = 1;
  273. }
  274. var ishave = tablelist.Where(i => i.Id == uuid && i.Sudutype == sudutype).Single();
  275. //旧行修改
  276. if (updatalistold.Count == 0 || isis == 1)
  277. {
  278. updatalistold.Add(new T_jgjdistance_table() { Id = uuid, Isnew = typ, Zch = ishave.Zch, Dcqc = ishave.Dcqc, Gdjxgjj = ishave.Gdjxgjj, Sudutype = sudutype });
  279. }
  280. loopProp(ref updatalistold, uuid, val, nana);
  281. loopProp(ref updatalistall, uuid, val, nana);
  282. }
  283. }
  284. private void loopProp(ref List<T_jgjdistance_table> updatalist,string uuid,string value,string nana)
  285. {
  286. for (int i = 0; i < updatalist.Count; i++)
  287. {
  288. if (updatalist[i].Id == uuid)
  289. {
  290. Type t = updatalist[i].GetType();//获得该类的Type
  291. foreach (PropertyInfo pi in t.GetProperties())
  292. {
  293. if (pi.Name == nana)
  294. {
  295. pi.SetValue(updatalist[i], Convert.ToDecimal(value));
  296. }
  297. }
  298. }
  299. }
  300. }
  301. private void JHjgjFrom_FormClosed(object sender, FormClosedEventArgs e)
  302. {
  303. DataAnalysisToPG.ClearCacheJGJXGJJ();
  304. }
  305. private void SaveBtn_Click(object sender, EventArgs e)
  306. {
  307. //保存
  308. if (DSTabControl.SelectedTabPageIndex == 0)
  309. {
  310. //高速
  311. saveData(0, ref GSnew, ref GSedit, ref GS, GSgridControl);
  312. }
  313. else
  314. {
  315. //普速
  316. saveData(1, ref DSnew, ref DSedit, ref DS, DSgridControl);
  317. }
  318. }
  319. private void saveData(int type,ref List<T_jgjdistance_table> data ,ref List<T_jgjdistance_table> data1, ref List<T_jgjdistance_table> alldata, GridControl GC)
  320. {
  321. foreach (var item in data.Concat(data1).ToList())
  322. {
  323. DataAnalysisToPG.UpdataJGJXGJJ(false, item);
  324. }
  325. //清除临时数据
  326. DataAnalysisToPG.ClearCacheJGJXGJJ();
  327. data.Clear();
  328. data1.Clear();
  329. //更新显示
  330. tablelist = DataAnalysisToPG.QueryJGJXGJJ();
  331. alldata = tablelist.Where(i => i.Sudutype == type).ToList();
  332. initShow(GC, alldata, data);
  333. XtraMessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  334. }
  335. ///// <summary>
  336. ///// 添加txt数据
  337. ///// </summary>
  338. //private void sendData()
  339. //{
  340. // OpenFileDialog opf = new OpenFileDialog();
  341. // opf.Multiselect = true;
  342. // if (opf.ShowDialog() == DialogResult.OK)
  343. // {
  344. // foreach (var item in opf.FileNames)
  345. // {
  346. // seadtxt(item);
  347. // }
  348. // }
  349. //}
  350. //public void seadtxt(string txtPath)
  351. //{
  352. // int type = Path.GetFileNameWithoutExtension(txtPath) == "高速" ? 0: 1;
  353. // StreamReader SR = new StreamReader(txtPath);
  354. // while (!SR.EndOfStream)
  355. // {
  356. // string[] str = SR.ReadLine().Replace(" ", "\t").Split('\t');
  357. // T_jgjdistance_table data = new T_jgjdistance_table();
  358. // data.Zch = Convert.ToDecimal(str[0].Trim());
  359. // data.Dcqc = Convert.ToDecimal(str[1].Trim());
  360. // data.Gdjxgjj = Convert.ToDecimal(str[2].Trim());
  361. // data.Sudutype = type;
  362. // DataAnalysisToPG.InsertJGJXGJJ(data);
  363. // }
  364. //}
  365. }
  366. }