Solved

vb.net get datagridview column into an array

Posted on 2013-05-26
7
2,884 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net 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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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