123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- 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
- {
- public Form_Main m_MainForm = null;
- //国际化
- Language lan;
- System.Collections.Hashtable table_GroupId;
- string MineralGroupDBAddress = Application.StartupPath + "\\Config\\SysData\\" + "OTSCleanlinesSTD.db";
- Dictionary<int,STDGroups> MineralGroupDictionary = new Dictionary<int, STDGroups>();
- //Dictionary<int, STDGroups> MineralGroupDictionaryInitial = new Dictionary<int, STDGroups>();
- /// <summary>
- ///数据库修改状态
- /// </summary>
- public enum DBInfoState
- {
- Add = 0,
- Modify = 1,
- Delete = 2
- }
- object Clone(Dictionary<int, STDGroups> Dictionary)
- {
- Dictionary<int, STDGroups> DictionaryInitial = new Dictionary<int, STDGroups>();
- foreach (var key in Dictionary.Keys)
- {
- STDGroups DGroups = new STDGroups();
- DGroups = DGroups.Clone(Dictionary[key]) as STDGroups;
- DictionaryInitial.Add(key,DGroups);
- }
- return DictionaryInitial;
- }
- 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)
- {
- STDGroups new_data = new STDGroups();
- new_data.name = "Default";
- new_data.color = "#C9C9C9";
- new_data.iorder = 999;
- MineralGroupDictionary.Add(0, new_data);
-
- if (dt.Rows.Count > 0)
- {
- foreach (DataRow item in dt.Rows)
- {
- if (item["name"].ToString() != "Default")
- {
- 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 = 2; i < Grid_FroupId.RowsCount; i++)
- {
- MineralGroupDictionary[(int)Grid_FroupId[i, 0].Tag].iorder = i-1;
- }
- System.Data.SQLite.SQLiteConnection m_dbConnection = new System.Data.SQLite.SQLiteConnection("data source='" + DBAddress + "'");
- m_dbConnection.Open();
- foreach (KeyValuePair<int, STDGroups> 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("Failed to add into database!", "Tip");
- }
- }
- else
- {
- if (!UpdataInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder))
- {
- MessageBox.Show("Failed to update to database!", "Tip");
- }
- }
- }
- else if (infoState == (int)DBInfoState.Delete)
- {
- if (!DeleteInDB(m_dbConnection, kv.Key, kv.Value.name, kv.Value.color, kv.Value.iorder))
- {
- MessageBox.Show("Failed to delete data in database!", "Tip");
- }
- }
- }
- m_dbConnection.Close();
- }
- public Form_GroupId(string DBAddress, Form_Main mainForm)
- {
- InitializeComponent();
- MineralGroupDBAddress = DBAddress;
- m_MainForm = mainForm;
- }
- private void Form_GroupId_Load(object sender, EventArgs e)
- {
- lan = new Language(this);
- table_GroupId = lan.GetNameTable("Form_GroupId");
- button_up.Enabled = false;
- button_down.Enabled = false;
- 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);
- Grid_FroupId[1, 0] = new SourceGrid.Cells.Cell("Default", typeof(string));
- Grid_FroupId[1, 1] = new SourceGrid.Cells.Cell("", typeof(string));
- Grid_FroupId.Rows[1].Height = 25;
- Grid_FroupId[1, 0].Tag = 0;
- SourceGrid.Cells.Views.Cell vw = new SourceGrid.Cells.Views.Cell();
- vw.BackColor = Attributes.colorHx16toRGB("#C9C9C9");
- Grid_FroupId[1, 1].View = vw;
- Grid_FroupId[1, 0].AddController(SourceGrid.Cells.Controllers.Unselectable.Default);
- Grid_FroupId[1, 1].AddController(SourceGrid.Cells.Controllers.Unselectable.Default);
- int i = 2;
- foreach (KeyValuePair<int, STDGroups> kv in MineralGroupDictionary)
- {
- if (kv.Key != 0)
- {
- 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("Failed to read database", "Tip");
- }
- }
- 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<int, STDGroups> 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("The names of groups are reusable which named " + DuplicateNames + ",please modify!", "Tip");
- 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))
- {
- bool bexist = false;
- foreach (KeyValuePair<int, STDdata> kv in m_MainForm.STDDictionary)
- {
- if(kv.Value.GroupId.ToString()== Grid_FroupId[x, 0].Tag.ToString())
- {
- bexist = true;
- break;
- }
- }
- if(bexist)
- {
- MessageBox.Show("There is a grouping in the user standard library that you want to delete!");
- return;
- }
- 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("No gruop is selected!", "Tip");
- }
- }
- 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 == ls_gd.RowsCount - 1)
- {
- button_down.Enabled = false;
- }
- else
- {
- button_down.Enabled = true;
- }
- if (i == 1)
- {
- button_up.Enabled = false;
- //MessageBox.Show("Group by default, not editable!");
- //return;
- }
- else
- {
- button_up.Enabled = true;
- }
-
- }
- }
- }
- }
|