Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a dataGridView which I want to import in my excel file. But I want the position of the gridview at the excel file in Row 14, Column 1.

I have this code:
VB
Dim Row As Integer = 14

For i = 0 To grdvwInterestBreakdown.RowCount - 1
   For j = 0 To grdvwInterestBreakdown.ColumnCount - 1
      xlworksheet.Cells(Row, j + 1) = _
         grdvwInterestBreakdown(j, i).Value.ToString()
   Next
   Row = Row + 1
Next

But it keeps on returning this error:
Object reference not set to an instance of an object.

Is there something missing in my codes or is there another way to import data with fixed position?

Thank you for the help.
Posted
Updated 27-Jul-14 21:59pm
v6
Comments
nilesh sawardekar 28-Jul-14 1:44am    
Values of Row and Col ?
mitchiee1226 28-Jul-14 2:45am    
Updated the question for you. Forgot to put the declaration of Row and Col.

xlworksheet seems to be Nothing - check it twice!
 
Share this answer
 
Comments
Maciej Los 28-Jul-14 4:46am    
Good point! +5!
mitchiee1226 28-Jul-14 4:53am    
Thanks for the help and patience. @Maciej Los
Maciej Los 28-Jul-14 5:03am    
You're welcome ;)
mitchiee1226 28-Jul-14 4:53am    
Thanks already solved it.
Replace:
VB
grdvwInterestBreakdown(j + 1, i + 1).Value.ToString()

with:
VB
grdvwInterestBreakdown(j, i).Value.ToString()


Index of cell in Worksheet starts with 1 (one), index of gridview starts with 0 (zero) to the count of rows -1.

I'll prefer to use ADO.NET. Have a look here: Accessing Microsoft Office Data from .NET Applications[^]
 
Share this answer
 
Comments
mitchiee1226 28-Jul-14 3:27am    
Tried your suggestion. But returns with an error: Object reference not set to an instance of an object.
Maciej Los 28-Jul-14 3:50am    
You did not. Please, read my comment to your second comment.
mitchiee1226 28-Jul-14 3:38am    
If grdvwInterestBreakdown(j + 1, i + 1).Value.ToString() has no value will it return this error?? Object reference not set to an instance of an object.
Maciej Los 28-Jul-14 3:50am    
Please, read my answer carefully and do exactly what i wrote.
mitchiee1226 28-Jul-14 3:57am    
Sorry I do not understand ADO.NET
I have tried changing the code just as you have said in your solution but still returns the same error.
VB
Dim Row As Integer = 13

For i = 0 To grdvwInterestBreakdown.RowCount - 2 
   For j = 0 To grdvwInterestBreakdown.ColumnCount - 1
      xlworksheet.Cells(Row, j + 1) = grdvwInterestBreakdown(j, i).Value.ToString()
   Next
   Row = Row + 1
Next


grdvwInterestBreakdown.RowCount - 1 - Returns the last row which has no value.
When you have 20 rows in the gridview, two of the last row is null that is why it returns this error:
Object reference not set to an instance of an object
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900