Results 1 to 5 of 5

Thread: export datagrid to csv

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2020
    Posts
    1

    export datagrid to csv

    good evening.
    vineyard ask for help how can i export custom columns and only the checked rows from a datagridview to csv

  2. #2
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: export datagrid to csv

    hello,

    have a look here and here, you may find ideas and tips

    Regards
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: export datagrid to csv

    If you want to export all columns excluding the check column see the following.

    https://gist.github.com/karenpayneor...aae17bdbce33b7

    This will not handle partial column list.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: export datagrid to csv

    Quote Originally Posted by bsande View Post
    good evening.
    vineyard ask for help how can i export custom columns and only the checked rows from a datagridview to csv
    you didn't show how you load the Datagridview
    you could pass a Datatable to a DataSet

    then pick out the Columns
    Code:
     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            CSVExport("E:\TestFolder\xChris.csv", _
                        dset.Tables(0), _
                                New DataColumn() _
                                        {dset.Tables(0).Columns(0), _
                                        dset.Tables(0).Columns(2)})
        End Sub
    
        Private Sub CSVExport(ByVal filename As String, _
                              ByVal table As DataTable, _
                              ByVal cols As DataColumn())
    
            Dim sw As New IO.StreamWriter(filename)
            For Each drow As DataRow In table.Rows
                For i As Integer = 0 To cols.GetUpperBound(0)
                    sw.Write(drow(cols(i)).ToString & ";")
                Next
                sw.WriteLine()
            Next
            sw.Close()
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: export datagrid to csv

    Try this...

    Code:
    Dim contents As String = ""
    For r As Integer = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(r).IsNewRow Then Continue For
        If CBool(DataGridView1.Rows(r).Cells(yourCheckBoxComnIndex).Value) = True Then
            For c As Integer = 0 To DataGridView1.ColumnCount - 1
                If c < DataGridView1.ColumnCount - 1 Then
                    contents &= If(DataGridView1.Rows(r).Cells(c).Value IsNot Nothing, DataGridView1.Rows(r).Cells(c).Value.ToString, "") & ","
                Else
                    contents &= If(DataGridView1.Rows(r).Cells(c).Value IsNot Nothing, DataGridView1.Rows(r).Cells(c).Value.ToString, "") & vbCrLf
                End If
            Next
        End If
    Next
    IO.File.WriteAllText("yourFilename.csv", contents)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width