Solved

VB.Net - DataGridView to Excel(trouble)

Posted on 2013-01-10
8
299 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
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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