Загрузка данных


using System;
using System.Windows.Forms;

namespace IP
{
    public partial class Form1 : Form
    {
        int n;

        public Form1()
        {
            InitializeComponent();
        }

        // СОЗДАТЬ МАТРИЦУ
        private void button1_Click(object sender, EventArgs e)
        {
            n = (int)numericUpDown1.Value;

            if (n <= 0)
            {
                textBoxErrors.Text = "Введите количество вершин > 0";
                return;
            }

            dataGridView1.ColumnCount = n;
            dataGridView1.RowCount = n;

            for (int i = 0; i < n; i++)
            {
                dataGridView1.Columns[i].HeaderText = (i + 1).ToString();
                dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
            }

            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    dataGridView1[j, i].Value = 0;

            listBox1.Items.Clear();
            textBoxErrors.Clear();
        }

        // ОЧИСТИТЬ
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Columns.Clear();
            listBox1.Items.Clear();
            textBoxErrors.Clear();
        }

        // ПРОВЕРКА ВВОДА
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int val = Convert.ToInt32(dataGridView1[e.ColumnIndex, e.RowIndex].Value);

                if (val != 0 && val != 1)
                {
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = 0;
                    textBoxErrors.Text = "Можно вводить только 0 или 1!";
                }
            }
            catch
            {
                dataGridView1[e.ColumnIndex, e.RowIndex].Value = 0;
                textBoxErrors.Text = "Ошибка ввода!";
            }
        }

        // НАЙТИ ИЗОЛИРОВАННЫЕ ВЕРШИНЫ
        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            textBoxErrors.Clear();

            if (dataGridView1.RowCount == 0)
            {
                textBoxErrors.Text = "Сначала создайте матрицу!";
                return;
            }

            int count = 0;

            for (int i = 0; i < n; i++)
            {
                bool isolated = true;

                for (int j = 0; j < n; j++)
                {
                    int row = Convert.ToInt32(dataGridView1[j, i].Value);
                    int col = Convert.ToInt32(dataGridView1[i, j].Value);

                    if (row != 0 || col != 0)
                    {
                        isolated = false;
                        break;
                    }
                }

                if (isolated)
                {
                    listBox1.Items.Add("Вершина №" + (i + 1));
                    count++;
                }
            }

            listBox1.Items.Add("----------------");
            listBox1.Items.Add("Количество: " + count);
        }
    }
}