3 Replies - 489 Views - Last Post: 24 July 2019 - 12:11 PM Rate Topic: -----

#1 Samuel75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-July 18

I want to Export datagridview to csv or excell in visual basic

Posted 24 July 2019 - 11:31 AM

I created application with sql as database and I want to export datagridview to csv/excel but I receive error
when I run it. I attached the code
[Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
        'create empty string
        Dim Thecsvfile As String = String.Empty

        'Get the Column headers.
        For Each Column As DataGridViewColumn In My_ContactsDataGridView.Columns
            Thecsvfile = Thecsvfile & Column.HeaderText & ","

        Next
        'Trim the last comma
        Thecsvfile = Thecsvfile.TrimEnd(",")
        'Add line to the output
        Thecsvfile = Thecsvfile & vbCr & vbLf
        'Get the rows
        For Each Row As DataGridViewRow In My_ContactsDataGridView.Rows

        Next
        'get the cells
        For Each cell As DataGridViewCell In Row.Cells
            Thecsvfile = Thecsvfile & cell.FormattedValue & ","

        Next
        'Trim the last comma
        Thecsvfile = Thecsvfile.TrimEnd(",")
        'Add line to the output
        Thecsvfile = Thecsvfile & vbCr & vbLf

        On Error Resume Next
        'write the file
        My.Computer.FileSystem.WriteAllText("export.csv", Thecsvfile, False)

    End Sub
End Classcode]
:code:

This post has been edited by modi123_1: 24 July 2019 - 11:39 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.


Is This A Good Question/Topic? 0
  • +

Replies To: I want to Export datagridview to csv or excell in visual basic

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,197
  • Joined: 12-June 08

Re: I want to Export datagridview to csv or excell in visual basic

Posted 24 July 2019 - 11:40 AM

Please copy/paste the complete error message.
Was This Post Helpful? 0
  • +
  • -

#3 Samuel75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 12-July 18

Re: I want to Export datagridview to csv or excell in visual basic

Posted 24 July 2019 - 12:03 PM

View Postmodi123_1, on 24 July 2019 - 11:40 AM, said:

Please copy/paste the complete error message.


line 19 on the code I posted and below is error

[Object variable or With block variable not set.][/code]
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,197
  • Joined: 12-June 08

Re: I want to Export datagridview to csv or excell in visual basic

Posted 24 July 2019 - 12:11 PM

A quick look says object 'Row' doesn't exist in that loop's scope.

19        For Each cell As DataGridViewCell In Row.Cells


Follow up - your datagrid should be backed by a dataset. That dataset object should be what you are looping through.. but that may be something to look at changing AFTER.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1