Solved

Error adding a row to a DataGridView?

Posted on 2014-01-22
3
352 Views
Last Modified: 2014-01-27
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
Comment
Question by:BlakeMcKenna
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39801176
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
 

Author Closing Comment

by:BlakeMcKenna
ID: 39812043
Sorry, forgot about this question. Your solution worked Fernando!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39812070
Not a problem BlakeMcKenna, glad to help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question