Solved

Error adding a row to a DataGridView?

Posted on 2014-01-22
3
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

742 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