A
A
asyasb2021-12-09 20:31:20
Arrays
asyasb, 2021-12-09 20:31:20

How to resolve the error (index out of range)?

When the program starts and when you enter n and m, on the line dataGridView1[i, j].Value = a[i,j]; - error (" index is out of range. Index must be a positive number, and its size must not exceed the size of the collection ") When n and m are equal to one, then everything works.

int n = Convert.ToInt32(textBox1.Text);
            int m = Convert.ToInt32(textBox2.Text);
            int [,] a = new int [n, m];
            Random r = new Random();
            dataGridView1.RowCount = n;
            dataGridView1.ColumnCount = m ;
            for (int i=0; i<n; i++)
                for (int j=0; j<m; j++)
                {
                    a[i, j] = r.Next(100);
                   
                }
           for (int i=0; i<n; i++)
                for (int j=0; j<m; j++)
                {
                    dataGridView1[i, j].Value = a[i,j];
                   
                }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question