private void InitializeGrid(int rows, int columns) { try { dataGridView1.ColumnCount = columns; dataGridView1.ColumnHeadersVisible = true; DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.BackColor = Color.Beige; columnHeaderStyle.Font = new Font("Times new roman", 12, FontStyle.Bold); dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle; for (int i = 0; i < columns; i++) { dataGridView1.Columns[i].Name = LetterNumberConverter.NumberToLetter(i); } dataGridView1.RowCount = rows; dataGridView1.RowHeadersVisible = true; DataGridViewCellStyle rowHeaderStyle = new DataGridViewCellStyle(); rowHeaderStyle.BackColor = Color.Beige; rowHeaderStyle.Font = new Font("Times new roman", 12, FontStyle.Bold); dataGridView1.ColumnHeadersDefaultCellStyle = rowHeaderStyle; for (int i = 0; i < rows; i++) { dataGridView1.Rows[i].HeaderCell.Value = (i).ToString(); } for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { DataCell cell = new DataCell(); cell.name_set(dataGridView1.Columns[j].Name + dataGridView1.Rows[i].HeaderCell.Value); cell.column = j; cell.row = i; Data.DataAdd(cell); } } Data.columns = columns; Data.rows = rows; } catch (Exception) { } }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { try { Stream mystream; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if (((mystream = saveFileDialog1.OpenFile()) != null)) { StreamWriter sw = new StreamWriter(mystream); sw.WriteLine(dataGridView1.RowCount); sw.WriteLine(dataGridView1.ColumnCount); for (int i = 0; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { if (dataGridView1.Rows[i].Cells[j].Value != null) { sw.WriteLine(dataGridView1.Rows[i].Cells[j].Value.ToString()); } else { sw.WriteLine(""); } } } for (int i = 0; i < dataGridView1.RowCount; i++) { for (int j = 0; j < dataGridView1.ColumnCount; j++) { string cell_name = LetterNumberConverter.NumberToLetter(j + 1) + (i + 1).ToString(); if (Data.GetData(i, j).expression_get() != null) { sw.WriteLine(Data.GetData(i, j).expression_get()); } else { sw.WriteLine(""); } } } sw.Close(); mystream.Close(); } } } catch (Exception) { MessageBox.Show("Saving Error"); } }
public static int NameToColumn(string s) { string temp = ""; string alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < s.Length; i++) { if (alphabet2.Contains(s[i])) { temp += s[i]; } } int m = LetterNumberConverter.LetterToNumber(temp); return(m); }
public int AddColumn(int _c, DataGridView dgv) { _c++; DataGridViewColumn column = (DataGridViewColumn)dgv.Columns[0].Clone(); column.HeaderCell.Value = LetterNumberConverter.NumberToLetter(_c - 1); dgv.Columns.Add(column); for (int i = 0; i < dataGridView1.RowCount; i++) { DataCell cell = new DataCell(); cell.name_set(dataGridView1.Columns[_c - 1].Name + dataGridView1.Rows[i].HeaderCell.Value); cell.column = _c - 1; cell.row = i; Data.DataAdd(cell); } Data.columns++; return(0); }
public bool IsCycled() { try { List <string> temp = new List <string>(); temp.Add(name); int i = 0; bool flag = false; if (related.Count == 1) { if (name == related[0]) { value = "Error"; return(true); } } while (true) { int m = temp.Count; if (i == (m)) { break; } DataCell tempr = Data.GetData(LetterNumberConverter.NameToRaw(temp[i]), LetterNumberConverter.NameToColumn(temp[i])); for (int j = 0; j < tempr.related.Count; j++) { temp.Add(tempr.related[j]); } for (int j = 0; j < temp.Count; j++) { for (int h = j + 1; h < temp.Count; h++) { if (temp[j] == temp[h]) { flag = true; break; } } if (flag == true) { break; } } if (flag == true) { break; } i++; } string k = ""; for (int j = 0; j < temp.Count; j++) { k += temp[j]; //.ToString() + " "; } Console.WriteLine(k); if (flag == false) { return(false); } for (int j = 0; j < temp.Count; j++) { DataCell tempr = Data.GetData(LetterNumberConverter.NameToRaw(temp[j]), LetterNumberConverter.NameToColumn(temp[j])); tempr.value_set("Error"); } return(true); } catch (Exception) { return(true); } }
public bool MyCircle(int row, int column) //!!! { DataCell cell = Data.GetData(row, column); List <string> depList = new List <string>(); depList.Add(cell.name_get()); string searchableCell = cell.name_get(); string currentCell = searchableCell; bool withoutCircle = true; CircleRecursivHelper(cell); DataCell tempCell; void CircleRecursivHelper(DataCell cell1) { if (!withoutCircle) { return; } tempCell = Data.GetData(LetterNumberConverter.NameToRaw(currentCell), LetterNumberConverter.NameToColumn(currentCell)); while (tempCell.related.Count != 0) { if (!withoutCircle) { return; } foreach (var depend in tempCell.related) { currentCell = depend; if (currentCell == searchableCell) { withoutCircle = false; //MessageBox.Show("Circle"); return; } depList.Add(currentCell); if (withoutCircle == false) { return; } CircleRecursivHelper(Data.GetData(LetterNumberConverter.NameToRaw(currentCell), LetterNumberConverter.NameToColumn(currentCell))); if (withoutCircle == false) { return; } depList.Remove(currentCell); } if (withoutCircle == false) { return; } } } if (!withoutCircle) { Data.GetData(LetterNumberConverter.NameToRaw(searchableCell), LetterNumberConverter.NameToColumn(searchableCell)).value_set("Error"); // dataGridView1.Rows[int.Parse(searchableCell[1].ToString())].Cells[int.Parse((searchableCell[0] - 65).ToString())].Value = "#CIRCLE"; for (int i = 0; i < depList.Count; i++) { Data.GetData(LetterNumberConverter.NameToRaw(depList[i]), LetterNumberConverter.NameToColumn(depList[i])).value_set("Error"); } return(true); } else { return(false); } }
private string Preparing(string expression, int column, int row) { try { DataCell temp_ = Data.GetData(row, column); string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string operations = "+*/^incdecmoddiv-()"; // string numbers = "0123456789"; expression = NoSpaces(expression); List <string> temp3 = new List <string>(); string expression2 = "+" + expression + "+"; for (int i = 0; i < expression2.Length; i++) { if (alphabet.Contains(expression2[i])) { string temp = ""; for (; ((!operations.Contains(expression2[i]))); i++) { temp += expression2[i]; } if (!temp3.Contains(temp)) { temp3.Add(temp); } } } temp_.related = temp3; // if (MyCircle(row, column)) return "Error"; V2 if (temp_.IsCycled()) { return("Error"); } for (int i = 0; i < temp_.related.Count; i++) { if ((Data.GetData(LetterNumberConverter.NameToRaw(temp_.related[i]), LetterNumberConverter.NameToColumn(temp_.related[i]))) == null) { return("Error"); } string m; if (Data.GetData(LetterNumberConverter.NameToRaw(temp_.related[i]), LetterNumberConverter.NameToColumn(temp_.related[i])).value_get() == null) { m = "0"; } else if (Data.GetData(LetterNumberConverter.NameToRaw(temp_.related[i]), LetterNumberConverter.NameToColumn(temp_.related[i])).value_get() == "Error") { return("Error"); } else { m = Data.GetData(LetterNumberConverter.NameToRaw(temp_.related[i]), LetterNumberConverter.NameToColumn(temp_.related[i])).value_get(); } expression = expression.Replace(temp_.related[i], m); } return(expression); } catch (Exception) { return("Error"); } }