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

x
?
Solved

VB.Net - DataGridView to Excel(trouble)

Posted on 2013-01-10
8
Medium Priority
?
304 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
[X]
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
  • 4
8 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38765039
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
ID: 38766814
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
ID: 38766826
That is strange. First column should be index 0. Are you changing display index of columns?
0
Independent Software Vendors: 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

by:Jimbo99999
ID: 38767157
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
 
LVL 83

Expert Comment

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

Author Comment

by:Jimbo99999
ID: 38767355
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
ID: 38767659
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 2000 total points
ID: 38767881
Given that Grid is displaying the data from datatable, it would work but I would suggest looping through the datatable.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
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 …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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