using SourceGrid; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace OTSPartA_STDEditor { public partial class Form_GroupId : Form { //国际化 Language lan; System.Collections.Hashtable table_GroupId; string MineralGroupDBAddress = Application.StartupPath + "\\Config\\SysData\\" + "OTSCleanlinesSTD.db"; Dictionary MineralGroupDictionary = new Dictionary(); /// ///数据库修改状态 /// public enum DBInfoState { Add = 0, Modify = 1, Delete = 2 } bool LoadDataFromDb(string DBaddress) { try { System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBaddress + "'"); m_dbConnection.Open(); System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups order by iorder", m_dbConnection); DataSet ds = new DataSet(); m_dataAdapter.Fill(ds); DataTable dt = ds.Tables[0]; if (dt != null) { if (dt.Rows.Count > 0) { foreach (DataRow item in dt.Rows) { STDGroups new_STDdata = new STDGroups(); new_STDdata.name = item["name"].ToString(); new_STDdata.color = item["color"].ToString(); new_STDdata.iorder = int.Parse(item["iorder"].ToString()); MineralGroupDictionary.Add(int.Parse(item["id"].ToString()), new_STDdata); } } } m_dbConnection.Close(); return true; } catch(Exception ee) { MessageBox.Show(ee.ToString()); return false; } } private void clickEvent_Click(object sender, EventArgs e) { SourceGrid.CellContext context = (SourceGrid.CellContext)sender; int i = context.Position.Row; int j = context.Position.Column; /// 保证鼠标点击的GRID行和列是有效的 if (i >= 0 && j == 1) { ColorDialog cd = new ColorDialog(); cd.FullOpen = true;//自定义颜色界面打开 DialogResult result = cd.ShowDialog(); if (result == DialogResult.OK) { SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = cd.Color; Grid_FroupId[i, 1].View = view; 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); } } Grid_FroupId.Refresh(); } void clickEvent_ValueChanged(object sender, EventArgs e) { SourceGrid.CellContext context = (SourceGrid.CellContext)sender; int i = context.Position.Row; int j = context.Position.Column; if (i >= 0 && j == 0) { MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].name = Grid_FroupId[i, 0].Value.ToString(); MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].InfoState = (int)DBInfoState.Modify; } } //查询数据是否存在 protected bool WhetherExistsInDBById(System.Data.SQLite.SQLiteConnection m_dbConnection, int id) { bool selResult = false; System.Data.SQLite.SQLiteDataAdapter m_dataAdapter = new System.Data.SQLite.SQLiteDataAdapter("select * from STDGroups where id=" + id.ToString() + "", m_dbConnection); DataSet ds = new DataSet(); m_dataAdapter.Fill(ds); DataTable dt = ds.Tables[0]; if (dt != null) { if (dt.Rows.Count > 0) { selResult = true; } } return selResult; } bool AddIntoDB(System.Data.SQLite.SQLiteConnection m_dbConnection,int id,string name,string color,int iorder) { System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand(); try { string insertstr = "Insert into STDGroups(id,name,color,iorder)"; string aa = "values(" + id + ",'" + name + "','" + color + "'," + iorder+ ")"; cmm.CommandText = insertstr + aa; cmm.ExecuteNonQuery(); return true; } catch /*(Exception ex)*/ { return false; } } bool UpdataInDB(System.Data.SQLite.SQLiteConnection m_dbConnection, int id, string name, string color, int iorder) { System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand(); try { string insertstr = "update STDGroups set name='" + name + "',color='"+ color + "',iorder=" + iorder+ " where id=" + id + ";"; cmm.CommandText = insertstr; cmm.ExecuteNonQuery(); return true; } catch /*(Exception ex)*/ { return false; } } bool DeleteInDB(System.Data.SQLite.SQLiteConnection m_dbConnection, int id, string name, string color, int iorder) { System.Data.SQLite.SQLiteCommand cmm = m_dbConnection.CreateCommand(); try { string insertstr = "delete from STDGroups where id=" + id + ";"; cmm.CommandText = insertstr; cmm.ExecuteNonQuery(); return true; } catch /*(Exception ex)*/ { return false; } } void SaveDataIntoDB(string DBAddress) { for (int i = 1; i < Grid_FroupId.RowsCount; i++) { MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].iorder = i; } System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'"); m_dbConnection.Open(); foreach (KeyValuePair kv in MineralGroupDictionary) { int infoState = kv.Value.InfoState; if (infoState == (int)DBInfoState.Modify) { bool result = WhetherExistsInDBById(m_dbConnection, kv.Key); if (!result) { if (!AddIntoDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder)) { MessageBox.Show(table_GroupId["message1"].ToString(), table_GroupId["message32"].ToString()); } } else { if (!UpdataInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder)) { MessageBox.Show(table_GroupId["message2"].ToString(), table_GroupId["message32"].ToString()); } } } else if (infoState == (int)DBInfoState.Delete) { if (!DeleteInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder)) { MessageBox.Show(table_GroupId["message3"].ToString(), table_GroupId["message32"].ToString()); } } } m_dbConnection.Close(); } public Form_GroupId(string DBAddress) { InitializeComponent(); MineralGroupDBAddress = DBAddress; } private void Form_GroupId_Load(object sender, EventArgs e) { lan = new Language(this); table_GroupId = lan.GetNameTable("Form_GroupId"); bool result = LoadDataFromDb(MineralGroupDBAddress); if(result) { Grid_FroupId.Redim(MineralGroupDictionary.Count + 1, 2); Grid_FroupId.Rows.SetHeight(0, 25); Grid_FroupId.AutoStretchColumnsToFitWidth = true; Grid_FroupId.Selection.EnableMultiSelection = false; Grid_FroupId.FixedRows = 1; Grid_FroupId.Columns[0].Width = 190; Grid_FroupId.Columns[1].Width = 135; if (table_GroupId["language"].ToString() == "ZH") { Grid_FroupId[0, 0] = new SourceGrid.Cells.Cell("组名称", typeof(string)); Grid_FroupId[0, 1] = new SourceGrid.Cells.Cell("组颜色", typeof(string)); } else { Grid_FroupId[0, 0] = new SourceGrid.Cells.Cell("Group Name", typeof(string)); Grid_FroupId[0, 1] = new SourceGrid.Cells.Cell("Group Color", typeof(string)); } SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.Font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold); view.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter; view.BackColor = Color.LightGray; Grid_FroupId[0, 0].View = view; Grid_FroupId[0, 1].View = view; Grid_FroupId[0, 0].AddController(SourceGrid.Cells.Controllers.Unselectable.Default); Grid_FroupId[0, 1].AddController(SourceGrid.Cells.Controllers.Unselectable.Default); int i = 1; foreach (KeyValuePair kv in MineralGroupDictionary) { Grid_FroupId[i, 0] = new SourceGrid.Cells.Cell(kv.Value.name, typeof(string)); Grid_FroupId[i, 1] = new SourceGrid.Cells.Cell("", typeof(string)); Grid_FroupId.Rows[i].Height = 25; Grid_FroupId[i, 0].Tag = kv.Key; SourceGrid.Cells.Views.Cell vew = new SourceGrid.Cells.Views.Cell(); vew.BackColor = Attributes.colorHx16toRGB(kv.Value.color); Grid_FroupId[i, 1].View = vew; SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents(); clickEvent.ValueChanged += new EventHandler(clickEvent_ValueChanged); Grid_FroupId[i, 0].AddController(clickEvent); SourceGrid.Cells.Controllers.CustomEvents clickEvent2 = new SourceGrid.Cells.Controllers.CustomEvents(); clickEvent2.Click+= new EventHandler(clickEvent_Click); Grid_FroupId[i, 1].AddController(clickEvent2); i++; } } else { MessageBox.Show(table_GroupId["message4"].ToString(), table_GroupId["message32"].ToString()); } } public int AddMineralGroupDictionaryItem() { STDGroups new_MineralGroup = new STDGroups();//定义一个TreeNode节点对象 new_MineralGroup.name = "New Group"; new_MineralGroup.color = Attributes.colorRGBtoHx16(Color.Gray.R, Color.Gray.G, Color.Gray.B); new_MineralGroup.InfoState = (int)DBInfoState.Modify; int Id = 100; foreach (KeyValuePair kv in MineralGroupDictionary) { if (Id < kv.Key) { Id = kv.Key; } } MineralGroupDictionary.Add(Id + 1, new_MineralGroup); return Id + 1; } public void AddNewRow(int Id, string GroupName, Color color) { Grid_FroupId.Rows.Insert(Grid_FroupId.Rows.Count); Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0] = new SourceGrid.Cells.Cell(GroupName, typeof(string)); Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1] = new SourceGrid.Cells.Cell("", typeof(string)); Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0].Tag = Id; Grid_FroupId.Rows[Grid_FroupId.Rows.Count - 1].Height = 25; SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = color; Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1].View = view; SourceGrid.Cells.Controllers.CustomEvents clickEvent = new SourceGrid.Cells.Controllers.CustomEvents(); clickEvent.ValueChanged += new EventHandler(clickEvent_ValueChanged); Grid_FroupId[Grid_FroupId.Rows.Count - 1, 0].AddController(clickEvent); SourceGrid.Cells.Controllers.CustomEvents clickEvent2 = new SourceGrid.Cells.Controllers.CustomEvents(); clickEvent2.Click += new EventHandler(clickEvent_Click); Grid_FroupId[Grid_FroupId.Rows.Count - 1, 1].AddController(clickEvent2); Grid_FroupId.Refresh(); Position pos = new Position(Grid_FroupId.Rows.Count - 1, 0); Grid_FroupId.Selection.Focus(pos, true); } public void RemoveMineralGroupDictionaryItem(int Id) { //MineralGroupDictionary.Remove(Id); MineralGroupDictionary[Id].InfoState = (int)DBInfoState.Delete; } private void button_Cancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } private void button_OK_Click(object sender, EventArgs e) { for (int i = 1; i < Grid_FroupId.RowsCount; i++) { for (int j = i+1; j < Grid_FroupId.RowsCount; j++) { if(Grid_FroupId[i,0].ToString()== Grid_FroupId[j,0].ToString()) { Position pos = new Position(i, 0); Grid_FroupId.Selection.Focus(pos, true); string DuplicateNames = Grid_FroupId[i, 0].ToString(); MessageBox.Show(table_GroupId["message5"].ToString() + DuplicateNames + table_GroupId["message6"].ToString(), table_GroupId["message32"].ToString()); return; } } } SaveDataIntoDB(MineralGroupDBAddress); this.DialogResult = DialogResult.Yes; this.Close(); } private void 添加组ToolStripMenuItem_Click(object sender, EventArgs e) { int Id = AddMineralGroupDictionaryItem(); AddNewRow(Id, "New Group", Color.Gray); } private void 删除组ToolStripMenuItem_Click(object sender, EventArgs e) { int x = Grid_FroupId.Selection.ActivePosition.Row; int y = Grid_FroupId.Selection.ActivePosition.Column; if (x > 0 && (y == 0|| y == 1)) { RemoveMineralGroupDictionaryItem((int)Grid_FroupId[x, 0].Tag); Grid_FroupId.Rows.Remove(x); Grid_FroupId.Refresh(); if (Grid_FroupId.RowsCount > 1) { Position pos = new Position(1, 0); Grid_FroupId.Selection.Focus(pos, true); Grid_FroupId[1, 0].Grid.Select(); } } else { MessageBox.Show(table_GroupId["message7"].ToString(), table_GroupId["message32"].ToString()); } } private void button_up_Click(object sender, EventArgs e) { Grid_FroupId.Focus(true); int selrow = Grid_FroupId.Selection.ActivePosition.Row; int id = (int)Grid_FroupId[selrow, 0].Tag; int id2 = (int)Grid_FroupId[selrow - 1, 0].Tag; string GroupName = Grid_FroupId[selrow, 0].Value.ToString(); Color color = Grid_FroupId[selrow, 1].View.BackColor; Grid_FroupId[selrow, 0].Value = Grid_FroupId[selrow - 1, 0].Value; Grid_FroupId[selrow, 0].Tag = id2; SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = Grid_FroupId[selrow - 1, 1].View.BackColor; Grid_FroupId[selrow, 1].View = view; Grid_FroupId[selrow - 1, 0].Value = GroupName; Grid_FroupId[selrow - 1, 0].Tag = id; SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell(); view2.BackColor = color; Grid_FroupId[selrow - 1, 1].View = view2; MineralGroupDictionary[id].name = Grid_FroupId[selrow - 1, 0].Value.ToString(); MineralGroupDictionary[id2].name = Grid_FroupId[selrow, 0].Value.ToString(); Grid_FroupId.Refresh(); Position pos = new Position(selrow - 1, 0); Grid_FroupId[selrow - 1, 0].Grid.Select(); Grid_FroupId.Selection.Focus(pos, true); button_down.Enabled = true; if (selrow - 1 == 1) { button_up.Enabled = false; } } private void button_down_Click(object sender, EventArgs e) { Grid_FroupId.Focus(true); int selrow = Grid_FroupId.Selection.ActivePosition.Row; int id = (int)Grid_FroupId[selrow, 0].Tag; int id2 = (int)Grid_FroupId[selrow + 1, 0].Tag; string GroupName = Grid_FroupId[selrow, 0].Value.ToString(); Color color = Grid_FroupId[selrow, 1].View.BackColor; Grid_FroupId[selrow, 0].Value = Grid_FroupId[selrow + 1, 0].Value; Grid_FroupId[selrow, 0].Tag = (int)Grid_FroupId[selrow + 1, 0].Tag; SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = Grid_FroupId[selrow+1, 1].View.BackColor; Grid_FroupId[selrow, 1].View = view; Grid_FroupId[selrow + 1, 0].Value = GroupName; Grid_FroupId[selrow + 1, 0].Tag = id; SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell(); view2.BackColor = color; Grid_FroupId[selrow + 1, 1].View = view2; MineralGroupDictionary[id].name = Grid_FroupId[selrow + 1, 0].Value.ToString(); MineralGroupDictionary[id2].name = Grid_FroupId[selrow, 0].Value.ToString(); Grid_FroupId.Refresh(); Position pos = new Position(selrow + 1, 0); Grid_FroupId[selrow + 1, 0].Grid.Select(); Grid_FroupId.Selection.Focus(pos, true); button_up.Enabled = true; if (selrow + 1 == Grid_FroupId.RowsCount - 1) { button_down.Enabled = false; } } private void Grid_FroupId_Click(object sender, EventArgs e) { SourceGrid.Grid ls_gd = (SourceGrid.Grid)sender; ls_gd.Focus(); int i = ls_gd.Selection.ActivePosition.Row; int j = ls_gd.Selection.ActivePosition.Column; /// 保证鼠标点击的GRID行和列是有效的 if (i >= 0 && j >= 0) { if (i == 1) { button_up.Enabled = false; } else { button_up.Enabled = true; } if (i == ls_gd.RowsCount - 1) { button_down.Enabled = false; } else { button_down.Enabled = true; } } } } }