Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

Solved get datagridview column into an array

Posted on 2013-05-26
Medium Priority
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
Question by:PNRT
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
LVL 42

Expert Comment

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


Author Comment

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?
LVL 42

Expert Comment

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

Open in new window

Technology Partners: 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!


Author Comment

ID: 39197735
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
LVL 42

Expert Comment

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
            End If

Open in new window


Author Comment

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.
LVL 42

Accepted Solution

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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