Solved

vb.net get datagridview column into an array

Posted on 2013-05-26
7
2,796 Views
Last Modified: 2013-05-26
Hi Experts.  I have a datagridview where the first column is a list of numbers and need to get the column into an array (not an array list).

I'm trying

         Dim list() As Integer
         For RowIndex As Integer = 0 To DataGridView1.Rows.Count - 1 Step 1
            list(RowIndex) = DataGridView1(0, RowIndex).Value
        Next RowIndex

Open in new window



But keep getting the error   Object reference not set to an instance of an object.

I've tried many different methods but keep getting the same error

Any help would be appreciated
0
Comment
Question by:PNRT
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39197599
add Imports System.Linq
and use this code:

Dim arr As Integer() = DataGridView1.Rows.Cast(Of DataGridRow)().Select(Function(n) Int.Parse(n(0).Value)).ToArray()

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 39197673
Thanks But got errors
First was
Type 'DataGridRow' is not defined
Changed to DataGridViewRow
Then got
Overload resolution failed because no accessible 'Select' can be called with these arguments:      

Is it not possible without Linq?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39197697
try replace line 3 with:
list(RowIndex) = DataGridView1.Rows(RowIndex)(0).Value

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:PNRT
ID: 39197735
Thanks
But With
list(RowIndex) = DataGridView1.Rows(RowIndex)(0).Value
I get the error
Class 'System.Windows.Forms.DataGridViewRow' cannot be indexed because it has no default property

Sorry, no idea what its referring to
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39197809
  Dim list As List(Of Integer) = New List(Of Integer)

        For Each d In DataGridView1.Rows.Cast(Of DataGridViewRow)()
            Dim num As Integer
            If Int32.TryParse(d.Cells(0).Value, num) Then
                list.Add(num)
            End If
        Next

Open in new window

0
 
LVL 2

Author Comment

by:PNRT
ID: 39197823
Sorry, I mentioned in the first question above it had to be a simple array not a list as I need to send the array to a function that requires an array.

I didn't realize it would be so difficult just to get a column of numbers into an array.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39197832
Its not at all, add this after u populate the list:
  Dim arr As Integer() = list.ToArray()
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

13 Experts available now in Live!

Get 1:1 Help Now