Question Data Grid View results to. . . excel or

Styles

Member
Joined
Sep 23, 2010
Messages
16
Programming Experience
Beginner
All,

I tried searching both these Forums and Google and have not been able to find an answer to this question. If I missed something in the forums I apologize. I have a database loaded into Visual Studio 2008 (VB.NET). My Form has a datagrid listbox of table "A" and the 2nd is a datagrid view of the results of table "A" and table "B" by city. To explain that better the first table posted to the form in a listbox has cities and town codes. The 2nd table is based on company names, city, phone number and a number order. The number order I included to sort the companies into an order after it is organized by city. The results of the 2nd datagridview show the companies in each town after you click on the listbox. Is there a way to export these individual results by town to Excel or Word? I have tables already setup for each. Or can I create a Cystal Report? Companies are constantly taken on or off the list. The output needs to just take the company name and phone numbers and put them into column a and the rest of the form is hand written. Any help or direction would be appreciated thanks.
 
Budius,

I tried the code you have below and I encounter 2 different but related problems.

VB.NET:
 For Each MyRow As DataGridViewRow In DG.Rows
            For Each MyCell As DataGridCell In MyRow.Cells

                oExcel.Cells((MyRow.Index*3) + MyCell.Index + 5, 1) = CityDataGridView(MyRow.Index, MyCell .Index).Value.ToString  ' fill it up
                     ' (MyRow.Index*3) <<< so for each row the loop will jump another 3 lines on Excel
                     ' + MyCell.Index  <<< for each column, it will put one after the other
                     ' + 5             <<< jump an extra amount for your headers (I'm not sure if it's 5, change it to your hearts content)
            Next
        Next

If I copy and paste this code into my form as listed here I get the following;
Name 'DG' is not declared
I want to say this is why I didn't use this type of coding before as I got a similar error.
If I change the "DG.rows" to "CityDataGridView.Rows" I loose the error but get a new one "MyCell.Index"
'Index is not a member 'System.Windows.Forms.DataGridCell'

I am still searching Google for any help with this, but any input would be greatly appreciated. Thanks
 
DG is a generic name on my example, it stands for DataGridview, you gotta replace it by whatever you're calling your DGV.

oh yeah, look at that, Cells doesn't have index property... hmmm... I should have tested before posting, but anyway, after I've open visual studio, it took me around 7 seconds to find alternatives properties to use, I'm sure you can do it.
 
Back
Top