Solved

VB.Net - DataGridView to Excel(trouble)

Posted on 2013-01-10
8
297 Views
Last Modified: 2013-01-14
Good Day Experts!

I have been sepnding way too long trying to figure this issue out.  Please be patient as I try to explain the best I can.  

My grid is populated with data. I interogate each DataGridViewRow for the values and send it to Excel.  The issue I am having is one of my columns in the grid is appraently invisible to my code.  The column and data is viewable in the grid but when I iterate throug the DataGridViewRow it never sees that column of data.  As a result I get an index out of range error.  

I have attached my code for iterating through the grid.  When the Code gets to the ProNumber assignment,  it is not grabbing the ProNumber from the grid...it is grabbing the Direction which is the next column in the grid.

So, in summary, I need to figure out why the code is ingoring the ProNumber column of data in grid.

Thanks for the patience,
jimbo99999
CodeSnippet.doc
0
Comment
Question by:Jimbo99999
  • 4
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
You are starting from Cells(1) and not Cells(0). Are you ignoring Cells(0) intentionally? Like rows, column indices start from 0 as well.
0
 

Author Comment

by:Jimbo99999
Comment Utility
Thanks for responding. I remember switching that to start at 1 due to "odd" behavior that I cannot recall.  

But, looking at how my data was displayed in the grid, it made more sense to have ProNumber(the misbehaving column) 3rdin the grid so I moved the column.  I changed the cell references to start at (0) and something strange happened.  

That ProNumber that is the 3rd column in the grid is what was returned at (0) and my 1st column in the grid is what is returned at (1)!

This is so frustrating when this little project needed to be finished yesterday.

Thanks,
jimbo99999
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
That is strange. First column should be index 0. Are you changing display index of columns?
0
 

Author Comment

by:Jimbo99999
Comment Utility
I am not sure what you mean by that...so probably not.

I was able to get it to work but I have questions why!

First, I thought my code snippet just gets the data in the DataGrid and outputs to Excel.  

To get it to work I had to change my query that I run to fill the DataTable. I put that ProNumber field 3rd in the select list to match up with where I want it in the DataGrid.  
No matter where I put it in the select list, the DataGrid still loads with no problem.

As soon as I change the location of that ProNumber field in my select list that I use to fill my DataTable that fills the DataGrid, I get an error when I export to Excel!

Just really odd...I thought my code snippet is just looking at the DataGrid.  It shouldn't care how the data gets there.

Thanks,
jimbo99999
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
You can also loop through datatable to export it to excel instead of looping through the grid.
0
 

Author Comment

by:Jimbo99999
Comment Utility
I am wondering if somehow when I am referenceing the DataGrid that to do my export to Excel if it is attaching to the DataTable.  That order is different than the DataGrid order.
0
 

Author Comment

by:Jimbo99999
Comment Utility
In that code snippet I attached previously, I changed the indexes to reference the order of the data in the DataTable even though I am loope through the DataGrid(which has a different order)...now my Excel output is coming out in the correct order.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
Given that Grid is displaying the data from datatable, it would work but I would suggest looping through the datatable.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now