Solved

Error adding a row to a DataGridView?

Posted on 2014-01-22
3
342 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 62

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 62

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now