Form_GroupId.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using SourceGrid;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace OTSPartA_STDEditor
  12. {
  13. public partial class Form_GroupId : Form
  14. {
  15. //国际化
  16. Language lan;
  17. System.Collections.Hashtable table_GroupId;
  18. string MineralGroupDBAddress = Application.StartupPath + "\\Config\\SysData\\" + "OTSCleanlinesSTD.db";
  19. Dictionary<int,STDGroups> MineralGroupDictionary = new Dictionary<int, STDGroups>();
  20. /// <summary>
  21. ///数据库修改状态
  22. /// </summary>
  23. public enum DBInfoState
  24. {
  25. Add = 0,
  26. Modify = 1,
  27. Delete = 2
  28. }
  29. bool LoadDataFromDb(string DBaddress)
  30. {
  31. try
  32. {
  33. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBaddress + "'");
  34. m_dbConnection.Open();
  35. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups order by iorder", m_dbConnection);
  36. DataSet ds = new DataSet();
  37. m_dataAdapter.Fill(ds);
  38. DataTable dt = ds.Tables[0];
  39. if (dt != null)
  40. {
  41. if (dt.Rows.Count > 0)
  42. {
  43. foreach (DataRow item in dt.Rows)
  44. {
  45. STDGroups new_STDdata = new STDGroups();
  46. new_STDdata.name = item["name"].ToString();
  47. new_STDdata.color = item["color"].ToString();
  48. new_STDdata.iorder = int.Parse(item["iorder"].ToString());
  49. MineralGroupDictionary.Add(int.Parse(item["id"].ToString()), new_STDdata);
  50. }
  51. }
  52. }
  53. m_dbConnection.Close();
  54. return true;
  55. }
  56. catch(Exception ee)
  57. {
  58. MessageBox.Show(ee.ToString());
  59. return false;
  60. }
  61. }
  62. private void clickEvent_Click(object sender, EventArgs e)
  63. {
  64. SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
  65. int i = context.Position.Row;
  66. int j = context.Position.Column;
  67. /// 保证鼠标点击的GRID行和列是有效的
  68. if (i >= 0 && j == 1)
  69. {
  70. ColorDialog cd = new ColorDialog();
  71. cd.FullOpen = true;//自定义颜色界面打开
  72. DialogResult result = cd.ShowDialog();
  73. if (result == DialogResult.OK)
  74. {
  75. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  76. view.BackColor = cd.Color;
  77. Grid_FroupId[i, 1].View = view;
  78. MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].color = Attributes.colorRGBtoHx16(Grid_FroupId[i, 1].View.BackColor.R, Grid_FroupId[i, 1].View.BackColor.G, Grid_FroupId[i, 1].View.BackColor.B);
  79. }
  80. }
  81. Grid_FroupId.Refresh();
  82. }
  83. void clickEvent_ValueChanged(object sender, EventArgs e)
  84. {
  85. SourceGrid.CellContext context = (SourceGrid.CellContext)sender;
  86. int i = context.Position.Row;
  87. int j = context.Position.Column;
  88. if (i >= 0 && j == 0)
  89. {
  90. MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].name = Grid_FroupId[i, 0].Value.ToString();
  91. MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].InfoState = (int)DBInfoState.Modify;
  92. }
  93. }
  94. //查询数据是否存在
  95. protected bool WhetherExistsInDBById(System.Data.SQLite.SQLiteConnection m_dbConnection, int id)
  96. {
  97. bool selResult = false;
  98. System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups where id=" + id.ToString() + "", m_dbConnection);
  99. DataSet ds = new DataSet();
  100. m_dataAdapter.Fill(ds);
  101. DataTable dt = ds.Tables[0];
  102. if (dt != null)
  103. {
  104. if (dt.Rows.Count > 0)
  105. {
  106. selResult = true;
  107. }
  108. }
  109. return selResult;
  110. }
  111. bool AddIntoDB(System.Data.SQLite.SQLiteConnection m_dbConnection,int id,string name,string color,int iorder)
  112. {
  113. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  114. try
  115. {
  116. string insertstr = "Insert into STDGroups(id,name,color,iorder)";
  117. string aa = "values(" + id + ",'" + name + "','" + color + "'," + iorder+ ")";
  118. cmm.CommandText = insertstr + aa;
  119. cmm.ExecuteNonQuery();
  120. return true;
  121. }
  122. catch /*(Exception ex)*/
  123. {
  124. return false;
  125. }
  126. }
  127. bool UpdataInDB(System.Data.SQLite.SQLiteConnection m_dbConnection, int id, string name, string color, int iorder)
  128. {
  129. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  130. try
  131. {
  132. string insertstr = "update STDGroups set name='" + name + "',color='"+ color + "',iorder=" + iorder+ " where id=" + id + ";";
  133. cmm.CommandText = insertstr;
  134. cmm.ExecuteNonQuery();
  135. return true;
  136. }
  137. catch /*(Exception ex)*/
  138. {
  139. return false;
  140. }
  141. }
  142. bool DeleteInDB(System.Data.SQLite.SQLiteConnection m_dbConnection, int id, string name, string color, int iorder)
  143. {
  144. System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand();
  145. try
  146. {
  147. string insertstr = "delete from STDGroups where id=" + id + ";";
  148. cmm.CommandText = insertstr;
  149. cmm.ExecuteNonQuery();
  150. return true;
  151. }
  152. catch /*(Exception ex)*/
  153. {
  154. return false;
  155. }
  156. }
  157. void SaveDataIntoDB(string DBAddress)
  158. {
  159. for (int i = 1; i < Grid_FroupId.RowsCount; i++)
  160. {
  161. MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].iorder = i;
  162. }
  163. System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
  164. m_dbConnection.Open();
  165. foreach (KeyValuePair<int, STDGroups> kv in MineralGroupDictionary)
  166. {
  167. int infoState = kv.Value.InfoState;
  168. if (infoState == (int)DBInfoState.Modify)
  169. {
  170. bool result = WhetherExistsInDBById(m_dbConnection, kv.Key);
  171. if (!result)
  172. {
  173. if (!AddIntoDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder))
  174. {
  175. MessageBox.Show(table_GroupId["message1"].ToString(), table_GroupId["message32"].ToString());
  176. }
  177. }
  178. else
  179. {
  180. if (!UpdataInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder))
  181. {
  182. MessageBox.Show(table_GroupId["message2"].ToString(), table_GroupId["message32"].ToString());
  183. }
  184. }
  185. }
  186. else if (infoState == (int)DBInfoState.Delete)
  187. {
  188. if (!DeleteInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder))
  189. {
  190. MessageBox.Show(table_GroupId["message3"].ToString(), table_GroupId["message32"].ToString());
  191. }
  192. }
  193. }
  194. m_dbConnection.Close();
  195. }
  196. public Form_GroupId(string DBAddress)
  197. {
  198. InitializeComponent();
  199. MineralGroupDBAddress = DBAddress;
  200. }
  201. private void Form_GroupId_Load(object sender, EventArgs e)
  202. {
  203. lan = new Language(this);
  204. table_GroupId = lan.GetNameTable("Form_GroupId");
  205. bool result = LoadDataFromDb(MineralGroupDBAddress);
  206. if(result)
  207. {
  208. Grid_FroupId.Redim(MineralGroupDictionary.Count + 1, 2);
  209. Grid_FroupId.Rows.SetHeight(0, 25);
  210. Grid_FroupId.AutoStretchColumnsToFitWidth = true;
  211. Grid_FroupId.Selection.EnableMultiSelection = false;
  212. Grid_FroupId.FixedRows = 1;
  213. Grid_FroupId.Columns[0].Width = 190;
  214. Grid_FroupId.Columns[1].Width = 135;
  215. if (table_GroupId["language"].ToString() == "ZH")
  216. {
  217. Grid_FroupId[0, 0] = new SourceGrid.Cells.Cell("组名称", typeof(string));
  218. Grid_FroupId[0, 1] = new SourceGrid.Cells.Cell("组颜色", typeof(string));
  219. }
  220. else
  221. {
  222. Grid_FroupId[0, 0] = new SourceGrid.Cells.Cell("Group Name", typeof(string));
  223. Grid_FroupId[0, 1] = new SourceGrid.Cells.Cell("Group Color", typeof(string));
  224. }
  225. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  226. view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
  227. view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter;
  228. view.BackColor = Color.LightGray;
  229. Grid_FroupId[0, 0].View = view;
  230. Grid_FroupId[0, 1].View = view;
  231. Grid_FroupId[0, 0].AddController(SourceGrid.Cells.Controllers.Unselectable.Default);
  232. Grid_FroupId[0, 1].AddController(SourceGrid.Cells.Controllers.Unselectable.Default);
  233. int i = 1;
  234. foreach (KeyValuePair<int, STDGroups> kv in MineralGroupDictionary)
  235. {
  236. Grid_FroupId[i, 0] = new SourceGrid.Cells.Cell(kv.Value.name, typeof(string));
  237. Grid_FroupId[i, 1] = new SourceGrid.Cells.Cell("", typeof(string));
  238. Grid_FroupId.Rows[i].Height = 25;
  239. Grid_FroupId[i, 0].Tag = kv.Key;
  240. SourceGrid.Cells.Views.Cell vew = new SourceGrid.Cells.Views.Cell();
  241. vew.BackColor = Attributes.colorHx16toRGB(kv.Value.color);
  242. Grid_FroupId[i, 1].View = vew;
  243. SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents();
  244. clickEvent.ValueChanged += new EventHandler(clickEvent_ValueChanged);
  245. Grid_FroupId[i, 0].AddController(clickEvent);
  246. SourceGrid.Cells.Controllers.CustomEvents clickEvent2 = new SourceGrid.Cells.Controllers.CustomEvents();
  247. clickEvent2.Click+= new EventHandler(clickEvent_Click);
  248. Grid_FroupId[i, 1].AddController(clickEvent2);
  249. i++;
  250. }
  251. }
  252. else
  253. {
  254. MessageBox.Show(table_GroupId["message4"].ToString(), table_GroupId["message32"].ToString());
  255. }
  256. }
  257. public int AddMineralGroupDictionaryItem()
  258. {
  259. STDGroups new_MineralGroup = new STDGroups();//定义一个TreeNode节点对象
  260. new_MineralGroup.name = "New Group";
  261. new_MineralGroup.color = Attributes.colorRGBtoHx16(Color.Gray.R, Color.Gray.G, Color.Gray.B);
  262. new_MineralGroup.InfoState = (int)DBInfoState.Modify;
  263. int Id = 100;
  264. foreach (KeyValuePair<int, STDGroups> kv in MineralGroupDictionary)
  265. {
  266. if (Id < kv.Key)
  267. {
  268. Id = kv.Key;
  269. }
  270. }
  271. MineralGroupDictionary.Add(Id + 1, new_MineralGroup);
  272. return Id + 1;
  273. }
  274. public void AddNewRow(int Id, string GroupName, Color color)
  275. {
  276. Grid_FroupId.Rows.Insert(Grid_FroupId.Rows.Count);
  277. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0] = new SourceGrid.Cells.Cell(GroupName, typeof(string));
  278. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1] = new SourceGrid.Cells.Cell("", typeof(string));
  279. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0].Tag = Id;
  280. Grid_FroupId.Rows[Grid_FroupId.Rows.Count - 1].Height = 25;
  281. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  282. view.BackColor = color;
  283. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1].View = view;
  284. SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents();
  285. clickEvent.ValueChanged += new EventHandler(clickEvent_ValueChanged);
  286. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0].AddController(clickEvent);
  287. SourceGrid.Cells.Controllers.CustomEvents clickEvent2 = new SourceGrid.Cells.Controllers.CustomEvents();
  288. clickEvent2.Click += new EventHandler(clickEvent_Click);
  289. Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1].AddController(clickEvent2);
  290. Grid_FroupId.Refresh();
  291. Position pos = new Position(Grid_FroupId.Rows.Count - 1, 0);
  292. Grid_FroupId.Selection.Focus(pos, true);
  293. }
  294. public void RemoveMineralGroupDictionaryItem(int Id)
  295. {
  296. //MineralGroupDictionary.Remove(Id);
  297. MineralGroupDictionary[Id].InfoState = (int)DBInfoState.Delete;
  298. }
  299. private void button_Cancel_Click(object sender, EventArgs e)
  300. {
  301. this.DialogResult = DialogResult.Cancel;
  302. this.Close();
  303. }
  304. private void button_OK_Click(object sender, EventArgs e)
  305. {
  306. for (int i = 1; i < Grid_FroupId.RowsCount; i++)
  307. {
  308. for (int j = i+1; j < Grid_FroupId.RowsCount; j++)
  309. {
  310. if(Grid_FroupId[i,0].ToString()== Grid_FroupId[j,0].ToString())
  311. {
  312. Position pos = new Position(i, 0);
  313. Grid_FroupId.Selection.Focus(pos, true);
  314. string DuplicateNames = Grid_FroupId[i, 0].ToString();
  315. MessageBox.Show(table_GroupId["message5"].ToString() + DuplicateNames + table_GroupId["message6"].ToString(), table_GroupId["message32"].ToString());
  316. return;
  317. }
  318. }
  319. }
  320. SaveDataIntoDB(MineralGroupDBAddress);
  321. this.DialogResult = DialogResult.Yes;
  322. this.Close();
  323. }
  324. private void 添加组ToolStripMenuItem_Click(object sender, EventArgs e)
  325. {
  326. int Id = AddMineralGroupDictionaryItem();
  327. AddNewRow(Id, "New Group", Color.Gray);
  328. }
  329. private void 删除组ToolStripMenuItem_Click(object sender, EventArgs e)
  330. {
  331. int x = Grid_FroupId.Selection.ActivePosition.Row;
  332. int y = Grid_FroupId.Selection.ActivePosition.Column;
  333. if (x > 0 && (y == 0|| y == 1))
  334. {
  335. RemoveMineralGroupDictionaryItem((int)Grid_FroupId[x, 0].Tag);
  336. Grid_FroupId.Rows.Remove(x);
  337. Grid_FroupId.Refresh();
  338. if (Grid_FroupId.RowsCount > 1)
  339. {
  340. Position pos = new Position(1, 0);
  341. Grid_FroupId.Selection.Focus(pos, true);
  342. Grid_FroupId[1, 0].Grid.Select();
  343. }
  344. }
  345. else
  346. {
  347. MessageBox.Show(table_GroupId["message7"].ToString(), table_GroupId["message32"].ToString());
  348. }
  349. }
  350. private void button_up_Click(object sender, EventArgs e)
  351. {
  352. Grid_FroupId.Focus(true);
  353. int selrow = Grid_FroupId.Selection.ActivePosition.Row;
  354. int id = (int)Grid_FroupId[selrow, 0].Tag;
  355. int id2 = (int)Grid_FroupId[selrow - 1, 0].Tag;
  356. string GroupName = Grid_FroupId[selrow, 0].Value.ToString();
  357. Color color = Grid_FroupId[selrow, 1].View.BackColor;
  358. Grid_FroupId[selrow, 0].Value = Grid_FroupId[selrow - 1, 0].Value;
  359. Grid_FroupId[selrow, 0].Tag = id2;
  360. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  361. view.BackColor = Grid_FroupId[selrow - 1, 1].View.BackColor;
  362. Grid_FroupId[selrow, 1].View = view;
  363. Grid_FroupId[selrow - 1, 0].Value = GroupName;
  364. Grid_FroupId[selrow - 1, 0].Tag = id;
  365. SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell();
  366. view2.BackColor = color;
  367. Grid_FroupId[selrow - 1, 1].View = view2;
  368. MineralGroupDictionary[id].name = Grid_FroupId[selrow - 1, 0].Value.ToString();
  369. MineralGroupDictionary[id2].name = Grid_FroupId[selrow, 0].Value.ToString();
  370. Grid_FroupId.Refresh();
  371. Position pos = new Position(selrow - 1, 0);
  372. Grid_FroupId[selrow - 1, 0].Grid.Select();
  373. Grid_FroupId.Selection.Focus(pos, true);
  374. button_down.Enabled = true;
  375. if (selrow - 1 == 1)
  376. {
  377. button_up.Enabled = false;
  378. }
  379. }
  380. private void button_down_Click(object sender, EventArgs e)
  381. {
  382. Grid_FroupId.Focus(true);
  383. int selrow = Grid_FroupId.Selection.ActivePosition.Row;
  384. int id = (int)Grid_FroupId[selrow, 0].Tag;
  385. int id2 = (int)Grid_FroupId[selrow + 1, 0].Tag;
  386. string GroupName = Grid_FroupId[selrow, 0].Value.ToString();
  387. Color color = Grid_FroupId[selrow, 1].View.BackColor;
  388. Grid_FroupId[selrow, 0].Value = Grid_FroupId[selrow + 1, 0].Value;
  389. Grid_FroupId[selrow, 0].Tag = (int)Grid_FroupId[selrow + 1, 0].Tag;
  390. SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell();
  391. view.BackColor = Grid_FroupId[selrow+1, 1].View.BackColor;
  392. Grid_FroupId[selrow, 1].View = view;
  393. Grid_FroupId[selrow + 1, 0].Value = GroupName;
  394. Grid_FroupId[selrow + 1, 0].Tag = id;
  395. SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell();
  396. view2.BackColor = color;
  397. Grid_FroupId[selrow + 1, 1].View = view2;
  398. MineralGroupDictionary[id].name = Grid_FroupId[selrow + 1, 0].Value.ToString();
  399. MineralGroupDictionary[id2].name = Grid_FroupId[selrow, 0].Value.ToString();
  400. Grid_FroupId.Refresh();
  401. Position pos = new Position(selrow + 1, 0);
  402. Grid_FroupId[selrow + 1, 0].Grid.Select();
  403. Grid_FroupId.Selection.Focus(pos, true);
  404. button_up.Enabled = true;
  405. if (selrow + 1 == Grid_FroupId.RowsCount - 1)
  406. {
  407. button_down.Enabled = false;
  408. }
  409. }
  410. private void Grid_FroupId_Click(object sender, EventArgs e)
  411. {
  412. SourceGrid.Grid ls_gd = (SourceGrid.Grid)sender;
  413. ls_gd.Focus();
  414. int i = ls_gd.Selection.ActivePosition.Row;
  415. int j = ls_gd.Selection.ActivePosition.Column;
  416. /// 保证鼠标点击的GRID行和列是有效的
  417. if (i >= 0 && j >= 0)
  418. {
  419. if (i == 1)
  420. {
  421. button_up.Enabled = false;
  422. }
  423. else
  424. {
  425. button_up.Enabled = true;
  426. }
  427. if (i == ls_gd.RowsCount - 1)
  428. {
  429. button_down.Enabled = false;
  430. }
  431. else
  432. {
  433. button_down.Enabled = true;
  434. }
  435. }
  436. }
  437. }
  438. }