Exemple #1
0
        private void btnNColumns_Click(object sender, EventArgs e)
        {
            var columnsAmount = dataGridView1.Columns.Count;
            var N             = Convert.ToInt32(txtbxNRowsColumns.Text);

            if (rbtnAddRowColumn.Checked == true)
            {
                for (var i = 1; i <= N; i++)
                {
                    string columnName = HelpfulFunctions.GetColNameFromIndex(columnsAmount + i);
                    dataGridView1.Columns.Add(columnName, columnName);

                    gridData1.AddCol();
                }

                if (gridData1.GetColAmount() == 1)
                {
                    gridData1.AddRow();
                }
                UpdateLabelAboutRowsAndColumns();
            }
            else
            {
                if (N <= columnsAmount)
                {
                    for (var i = 0; i < N; i++)
                    {
                        string columnName = HelpfulFunctions.GetColNameFromIndex(columnsAmount - i);
                        dataGridView1.Columns.Remove(columnName);

                        gridData1.DeleteCol();
                    }

                    if (dataGridView1.Columns.Count == 0)
                    {
                        for (int i = 0; i < gridData1.GetRowAmount(); i++)
                        {
                            gridData1.DeleteRow();
                        }
                    }
                    UpdateLabelAboutRowsAndColumns();
                }
                else
                {
                    MessageBox.Show("Attempt to delete more columns than table has");
                }
            }
        }
Exemple #2
0
        public void OpenFile(string path)
        {
            for (int a = 0; a < gridData1.GetColAmount(); a++)
            {
                for (int b = 0; b < gridData1.GetRowAmount(); b++)
                {
                    dataGridView1.Rows[b].Cells[a].Value = "";
                    gridData1.DeleteCell(a, b);
                }
            }
            for (int a = 0; a < gridData1.GetRowAmount(); a++)
            {
                dataGridView1.Columns.Remove(HelpfulFunctions.GetColNameFromIndex(a));
                gridData1.DeleteRow();
            }
            for (int a = 0; a < gridData1.GetColAmount(); a++)
            {
                dataGridView1.Rows.Remove(dataGridView1.Rows[a]);
                gridData1.DeleteCol();
            }


            if (!File.Exists(path))
            {
                throw new SystemException("THERE_IS_NO_FILE");
            }

            string fileData;

            fileData = File.ReadAllText(path);

            gridData1.AddRow();


            int rowIterator = 0;
            int colAmount   = 0;
            int rowAmount   = 0;
            int colIterator = 0;

            StringReader sr = new StringReader(fileData);
            string       line;

            while ((line = sr.ReadLine()) != null)
            {
                for (int i = 0; i < line.Length; i++)
                {
                    string cellText = "";
                    while (i < line.Length && line[i] != '|')
                    {
                        cellText += line[i];
                        i++;
                    }

                    if (i >= line.Length)
                    {
                        gridData1.AddRow();
                        dataGridView1.Rows.Add(1);
                        rowIterator++;
                        colIterator = 0;
                        break;
                    }
                    gridData1.AddCol();
                    dataGridView1.Columns.Add(HelpfulFunctions.GetColNameFromIndex(colIterator), HelpfulFunctions.GetColNameFromIndex(colIterator));
                    gridData1.ChangeCellExpression(colIterator, rowIterator, cellText);
                    colIterator++;
                }
            }
            dataGridView1.Rows.Add(1);

            gridData1.UpdateAllCells();
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    if (dataGridView1.Rows[j].Cells[i].Value != null)
                    {
                        if (FOD == FormatOfData.VALUE)
                        {
                            dataGridView1.Rows[j].Cells[i].Value = gridData1.GetCellValue(i, j);
                        }
                        else
                        {
                            dataGridView1.Rows[j].Cells[i].Value = gridData1.GetCellExpression(i, j);
                        }
                    }
                }
            }
        }