Solved

vb.net get datagridview column into an array

Posted on 2013-05-26
7
2,944 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…

733 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