Reputation: 45
I'm trying to add value of column from selected row on my datagridview to Collection(but I get same error if I do it with List or Array)
CODE:
Dim zdgv = MyDataGridView
For a = 0 To zdgv.SelectedRows.Count - 1
MsgBox(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
Try
MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
Catch ex As Exception
MsgBox(ex.Message)
MsgBox(ex.InnerException)
End Try
Next
ex.Message = Object reference not set to an instance of an object
ex.InnerException = empty
ex.InnerException.Message = Makes program crash, goes to code screen, highlights MsgBox(ex.InnerException) line, and gives error: Object reference not set to an instance of an object
ADDITIONAL INFO: Using QuickWatch on zdgv gives me all info. Using it on Rows after it(zdgv) says: 'Rows' is not declared. It may be inaccessible due to its protection level.
P.S. Yes I've googled, but none problem was similar. Yes I've searched here but no info. I've tryed r/visualbasic too - nothing... I've even tryed search for c# related stuff with this error - nothing. :/
Thanks in advance.
EDIT1: I've tryed make non-databound datagridview in new project, and add one value from it to collection - same error. I guess I should go google about "Setting Reference of Object to an Instance of an Object".
EDIT2: This one was fail - newbie mistake.
EDIT3: using quickwatch on
zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString
it shows right value(correct one, without throwing errors) = "1".
Upvotes: 4
Views: 26552
Reputation: 1
Best Solution I found Basically, the error is that your code is making use of a non-existing row.
You will merely need to turn to false the datagridview AllowUserToAddRows
property. Then all your normal loops will work properly.
Dim zdgv = DataGridView1
For Each row As DataGridViewRow In zdgv.Rows
ListBox2.Items.Add(row.Cells(1).Value.ToString)
Next
or
For i as integer = 0 to datagridView1.rows.count - 2
'enter code here
Next
MK :)
Upvotes: 0
Reputation: 8647
At top of code - just below public class classname and above first sub I've this: Public XXXXX As Collection
You don't' create an instance of collection
and then you try add some items to it.
It should be:
Public XXXXX As New Collection
Or you need to create a new instance somewhere else before access it
XXXXX = New Collection
Upvotes: 1
Reputation: 253
This code works like a charm on my side.
Did you forget a New on your MyCollection?
Dim zdgv = MyDataGridView
Dim MyCollection As New Collection
For a = 0 To zdgv.SelectedRows.Count - 1
MsgBox(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
Try
MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
Catch ex As Exception
MsgBox(ex.Message)
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End If
End Try
Next
Upvotes: 3
Reputation: 253
ex.InnerException is null, and you try to access is Message attribute. It's a normal behavior. You should try something like
Try
MyCollection.Add(zdgv.Rows(zdgv.SelectedRows(a).Index).Cells(0).Value.ToString)
Catch ex As Exception
MsgBox(ex.Message)
If ex.InnerException IsNot Nothing Then
MsgBox(ex.InnerException)
End if
End Try
InnerException is not null only if a sub method threw an exception under it.
Upvotes: 1