Out Of Range Exception when filling DataGridView



  • Please help me, why does the program always fly out of the wrong direction of the crossroads where the dataGrid its weights come from the mass?

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim A(,) As Integer, n As Integer, m As Integer
            Dim i As Integer, j, Gl, Pob As Integer
    
        Randomize() 'Иницилизация генератора случайных чисел
        m = InputBox("Введите число строк", , 10)
        n = InputBox("Введите число столбцов", , 10)
    
        ReDim A(0 To m, 0 To n) ' Размеры матрицы А -  m-строк, n-столбцов
        For i = 0 To m
            For j = 0 To n
                A(i, j) = Int(Rnd() * 21 - 10) 'Заполняем матрицу случайными числами
            Next j
        Next i
    
        DataGridView1.ColumnCount = n
        DataGridView1.RowCount = m
    
        For i = 0 To m
            'Идём по столбцам в строке'
            For j = 0 To n
                DataGridView1.Item(i, j).Value = A(i, j)
            Next j
        Next i
    
        For i = 0 To m
            For j = 0 To n
                If i = j Then Gl = Gl + A(i, j) ' считаем главную диагональ
                If i = m - j + 1 Then Pob = Pob + A(i, j) ' считаем побочную диагональ
            Next j
        Next i
    
        MsgBox("Сумма элементов главной диагонали = " & Gl, vbInformation + vbOKOnly, "Вывод ответа")
        MsgBox("Сумма элементов побочной диагонали = " & Pob, vbInformation + vbOKOnly, "Вывод ответа")
    End Sub
    



  • If the mass is 0, then to m-1 and n-1




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2