Solved

VB.Net - DataGridView to Excel(trouble)

Posted on 2013-01-10
8
302 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
Industry Leaders: 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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