export only visible columns from datagridview to a text file

T

TG

Hi!

This code works perfect except that it shows all the data from the
datagridview. I only want to export the columns that are visible.

How can I achieve this?

Thanks a lot!

Tammy



Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click

Dim writer As StreamWriter = New StreamWriter("R:\Spam BB
Search Engine Application\Exports\GridExport.txt")
If (DataGridView1.Rows.Count > 0) Then
For Each col As DataGridViewColumn In
DataGridView1.Columns
If (col.Index = (DataGridView1.Columns.Count - 1))
Then
writer.WriteLine(col.HeaderText)
Else
writer.Write(String.Concat(col.HeaderText, ","))
End If
Next
For Each row As DataGridViewRow In DataGridView1.Rows
'If Not omitIndices.Contains(row.Index) Then
For Each cell As DataGridViewCell In row.Cells
If (cell.OwningColumn.Index _
= (DataGridView1.Columns.Count - 1))
Then
If (Not (cell.Value) Is Nothing) Then
writer.WriteLine(cell.Value.ToString)
Else
writer.WriteLine("")
End If
ElseIf (Not (cell.Value) Is Nothing) Then

writer.Write(String.Concat(cell.Value.ToString, ","))
Else
writer.Write(String.Concat("", ","))
End If
Next
'End If
Next
End If
writer.Close()


End Sub
 
T

TG

Hi!

This code works perfect except that it shows all the data from the
datagridview. I only want to export the columns that are visible.

How can I achieve this?

Thanks a lot!

Tammy

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click

        Dim writer As StreamWriter = New StreamWriter("R:\Spam BB
Search Engine Application\Exports\GridExport.txt")
        If (DataGridView1.Rows.Count > 0) Then
            For Each col As DataGridViewColumn In
DataGridView1.Columns
                If (col.Index = (DataGridView1.Columns.Count - 1))
Then
                    writer.WriteLine(col.HeaderText)
                Else
                    writer.Write(String.Concat(col.HeaderText, ","))
                End If
            Next
            For Each row As DataGridViewRow In DataGridView1.Rows
                'If Not omitIndices.Contains(row.Index) Then
                For Each cell As DataGridViewCell In row.Cells
                    If (cell.OwningColumn.Index _
                                = (DataGridView1.Columns.Count - 1))
Then
                        If (Not (cell.Value) Is Nothing) Then
                            writer.WriteLine(cell.Value.ToString)
                        Else
                            writer.WriteLine("")
                        End If
                    ElseIf (Not (cell.Value) Is Nothing) Then

writer.Write(String.Concat(cell.Value.ToString, ","))
                    Else
                        writer.Write(String.Concat("", ","))
                    End If
                Next
                'End If
            Next
        End If
        writer.Close()

    End Sub



does anybody know how to do this?
 
B

Bill Schanks

In your column loop you could check for col.visible or col.width and
only write it to the text if it's visible or it's width is > 0. In
your row loop check for cell.OwningColumn.Visible or
cell.OwningColumn.Width.

I didn't test it, but that where I would start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top