[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 368
  • Last Modified:

Error adding a row to a DataGridView?

I don't know why I have so much trouble with DGV's but here's another one. I keep getting the following error (see attached image) on the line of code indicated.

    Private Sub LoadTestGrid()
        Try
            Dim col As DataGridViewColumn
            Dim cell As DataGridViewTextBoxCell

            With dgvEquip
                col = New DataGridViewColumn
                cell = New DataGridViewTextBoxCell
                col.CellTemplate = cell
                col.Width = 60
                col.HeaderText = "Unit"
                .Columns.Add(col)

                col = New DataGridViewColumn
                cell = New DataGridViewTextBoxCell
                col.CellTemplate = cell
                col.Width = 80
                col.HeaderText = "Output (" & cmbVoltage.SelectedText & ")"
                .Columns.Add(col)

                col = New DataGridViewColumn
                cell = New DataGridViewTextBoxCell
                col.CellTemplate = cell
                col.Width = 80
                col.HeaderText = "Pass/Fail"
                .Columns.Add(col)
            End With

            Dim row As DataGridViewRow

            row = New DataGridViewRow
            dgvEquip.Rows.Add(row)
            row.Cells(0).Value = 20
            row.Cells(1).Value = String.Empty
            row.Cells(2).Value = String.Empty

            row = New DataGridViewRow
            dgvEquip.Rows.Add(row)
            row.Cells(0).Value = 30       <====== Receive Error Message Here
            row.Cells(1).Value = String.Empty
            row.Cells(2).Value = String.Empty

            dgvEquip.CurrentCell = dgvEquip(1, 1)
            dgvEquip.Focus()

        Catch ex As Exception
            EH.ErrorMessage = "LoadTestGrid() - " & ex.Message & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

Screenshot.jpg
0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 2
1 Solution
 
Fernando SotoCommented:
Hi BlakeMcKenna;

The problem you are having is that these statements:

Dim row As DataGridViewRow          
row = New DataGridViewRow

Creates a new DataGridViewRow but does NOT use the columns defined in your dgvEquip and so ends up with zero columns in it so when you start filling in the columns you get the index out of range error. Try it like below.

' This creates a new row with all the columns defined and returns the index of that new row.
Dim rowIdx = dgvEquip.Rows.Add()
' This get a reference to that new row
Dim row = dgvEquip.Rows(rowIdx)
row.Cells(0).Value = 20
row.Cells(1).Value = String.Empty
row.Cells(2).Value = String.Empty

rowIdx = dgvEquip.Rows.Add()
row = dgvEquip.Rows(rowIdx)
row.Cells(0).Value = 30
row.Cells(1).Value = String.Empty
row.Cells(2).Value = String.Empty

Open in new window

0
 
BlakeMcKennaAuthor Commented:
Sorry, forgot about this question. Your solution worked Fernando!
0
 
Fernando SotoCommented:
Not a problem BlakeMcKenna, glad to help.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now