Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Go Back  Xtreme Visual Basic Talk > > > Exporting information from a DataGrid to an excel file or CSV.


Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2011, 01:36 PM
Satarian Satarian is offline
Newcomer
 
Join Date: Apr 2008
Posts: 19
Default Exporting information from a DataGrid to an excel file or CSV.


Hello All,

I've done several searches but I am not sure what terms I should be using to get the right results.

Also I may be using a datagrid for the wrong purpose so please let me know if that is the case as well

I am writing a program that uses several textboxes that a user will fill in and then click the add button to add the data into the datagrid. There is no backend database or anything. Just seeing the data in the program only.

What I want to do is then export this data from the datagrid into a file, preferably excel.

I've done copying textbox data into txt files but I can't find a method to copy datagrid data into a file.

I am using VS 2010.
If there is any other data you need please let me know.

Thanks in Advance.

Stephen
Reply With Quote
  #2  
Old 04-26-2011, 04:24 PM
AgentSmithers AgentSmithers is offline
Contributor
 
Join Date: Jan 2005
Location: SOCal
Posts: 492
Default

Hi Satarian,

Cycle through all Rows then while Cycling Cycle through all the Colums then dump the Data thats inside to a text file using a "," as a Dilimator.

CSV should be like this

COLUMN NAME 1,COLUMN NAME 2,COLUMN NAME 3
ROW 1 DATA,ROW 1 DATA,ROW 1 DATA
ROW 2 DATA,ROW 2 DATA,ROW 2 DATA
AND SO ON

Then Excel will open the Formatted Data.

Loading it is the exactly Visa Versa.

Code:
Private Sub SaveCVSToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveCVSToolStripMenuItem.Click
        If Not DataGridView1.Rows.Count > 1 Then
            MsgBox("No Data To Save")
            Exit Sub
        End If

        SaveFileDialog1.InitialDirectory = "C:\"
        SaveFileDialog1.Title = "Save a CSV File"
        SaveFileDialog1.FileName = vbNullString
        SaveFileDialog1.Filter = "CSV files (*.CSV)|*.CSV;*.XLS"
        SaveFileDialog1.DefaultExt = ".CSV"
        SaveFileDialog1.ShowDialog()

        If SaveFileDialog1.FileName <> "" Then
            Dim filestream = New System.IO.StreamWriter(SaveFileDialog1.FileName)

            'Apply the Header
            filestream.Write("Name, Description, Comment" & vbCrLf)

            For i = 0 To DataGridView1.Rows.Count - 2
                For ii = 0 To DataGridView1.Columns.Count - 2
                    If ii <> DataGridView1.Columns.Count - 2 Then
                        If Not IsNothing(DataGridView1.Item(ii, i).Value) Then
                            filestream.Write(DataGridView1.Item(ii, i).Value.ToString & ",")
                        Else
                            filestream.Write(",")
                        End If
                    Else
                        If Not IsNothing(DataGridView1.Item(ii, i).Value) Then
                            filestream.Write(DataGridView1.Item(ii, i).Value.ToString & vbCrLf)
                        Else
                            filestream.Write(vbCrLf)
                        End If
                    End If
                Next
            Next
            filestream.Close()
        End If
    End Sub
__________________
Http://ControllingTheInter.Net
My General Computer Forum, From Security To Programming And Back To Troubleshooting.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV. Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
 
Exporting information from a DataGrid to an excel file or CSV.
Exporting information from a DataGrid to an excel file or CSV.
 
-->