Solved

VB.Net - DataGridView to Excel(trouble)

Posted on 2013-01-10
8
303 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

626 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