Answered Index was out of range

  • Monday, April 30, 2012 10:27 PM
     
     

    Trying to input a high setpoint, low setpoint, a % load, and number of compressors, then calculate the temp that all compressors are off. Then build a datagridview with the staging of compressors from all off to all on for each compressor up to 10 compressors. I am trying to insert headers from 1 to 10 compressors (Variable) with the text "Comp1", "Comp2", etc....Two rows with header. I am trying to use an array with index number to insert the text, but that is where my problem happens. "Index was out of range".

    Here is my code

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Integer, num1, num2, num3, num4, alloff As Single, stg As Single

            TextBox1.Focus()
            num1 = TextBox1.Text
            num2 = TextBox2.Text
            num3 = TextBox3.Text
            num4 = TextBox4.Text
            DGV1.RowCount = 2
            DGV1.ColumnCount = (TextBox4.Text + 1)
            ' name list
            Dim hname(11) As String
            hname(0) = "Mode"
            hname(1) = "Comp1"
            hname(2) = "Comp2"
            hname(3) = "Comp3"
            hname(4) = "Comp4"
            hname(5) = "Comp5"
            hname(6) = "COMP6"
            hname(7) = "COMP7"
            hname(8) = "Comp8"
            hname(9) = "Comp9"
            hname(10) = "Comp10"
            ' Add Headers

            DGV1.ColumnHeadersVisible = True
            For i = 1 To num4

                DGV1.ColumnCount = i
                DGV1.Columns(i).HeaderText = hname(i)
            Next

    And a snapshot of the error........

All Replies

  • Monday, April 30, 2012 10:35 PM
     
     

    TG,

    It sounds like a parallel refrigeration system, but anyway...

    It's hard to say because of how you're doing it, but do keep in mind that the rows are zero-based so the first one is zero and the last one is one less than the actual quantity.


    Please call me Frank :)

  • Monday, April 30, 2012 11:09 PM
     
     Proposed Answer

    Try

    For i = 0 to num4 - 1

    DGV1.ColumnCount = i

    DVG1.Columns(i).HeaderText = hname(i)

    Next


    Ali Hamdar (alihamdar.com - www.ids.com.lb)

  • Tuesday, May 01, 2012 12:58 AM
     
     Answered

    Thanks Frank,

    I got it figured out by accident by removing the one statement that changed the column count from the original.


    Roger


  • Tuesday, May 01, 2012 12:59 AM
     
     

    Thanks Ali,

    I got it figured out by accident by removing the one statement that changed the column count from the original.


    Roger