export only visible columns from datagridview to a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    export only visible columns from datagridview to a text file

    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button5.Click

    Dim writer As StreamWriter = New StreamWriter("R :\Spam BB
    Search Engine Application\Exp orts\GridExport .txt")
    If (DataGridView1. Rows.Count 0) Then
    For Each col As DataGridViewCol umn In
    DataGridView1.C olumns
    If (col.Index = (DataGridView1. Columns.Count - 1))
    Then
    writer.WriteLin e(col.HeaderTex t)
    Else
    writer.Write(St ring.Concat(col .HeaderText, ","))
    End If
    Next
    For Each row As DataGridViewRow In DataGridView1.R ows
    'If Not omitIndices.Con tains(row.Index ) Then
    For Each cell As DataGridViewCel l In row.Cells
    If (cell.OwningCol umn.Index _
    = (DataGridView1. Columns.Count - 1))
    Then
    If (Not (cell.Value) Is Nothing) Then
    writer.WriteLin e(cell.Value.To String)
    Else
    writer.WriteLin e("")
    End If
    ElseIf (Not (cell.Value) Is Nothing) Then

    writer.Write(St ring.Concat(cel l.Value.ToStrin g, ","))
    Else
    writer.Write(St ring.Concat("", ","))
    End If
    Next
    'End If
    Next
    End If
    writer.Close()


    End Sub
  • TG

    #2
    Re: export only visible columns from datagridview to a text file

    On Jun 3, 4:18 pm, TG <jtam...@yahoo. comwrote:
    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(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button5.Click
    >
            Dim writer As StreamWriter = New StreamWriter("R :\Spam BB
    Search Engine Application\Exp orts\GridExport .txt")
            If (DataGridView1. Rows.Count 0) Then
                For Each col As DataGridViewCol umn In
    DataGridView1.C olumns
                    If (col.Index = (DataGridView1. Columns.Count - 1))
    Then
                        writer.WriteLin e(col.HeaderTex t)
                    Else
                        writer.Write(St ring.Concat(col .HeaderText, ","))
                    End If
                Next
                For Each row As DataGridViewRow In DataGridView1.R ows
                    'If Not omitIndices.Con tains(row.Index ) Then
                    For Each cell As DataGridViewCel l In row.Cells
                        If (cell.OwningCol umn.Index _
                                    = (DataGridView1. Columns.Count - 1))
    Then
                            If (Not (cell.Value) Is Nothing) Then
                                writer.WriteLin e(cell.Value.To String)
                            Else
                                writer.WriteLin e("")
                            End If
                        ElseIf (Not (cell.Value) Is Nothing) Then
    >
    writer.Write(St ring.Concat(cel l.Value.ToStrin g, ","))
                        Else
                            writer.Write(St ring.Concat("", ","))
                        End If
                    Next
                    'End If
                Next
            End If
            writer.Close()
    >
        End Sub


    does anybody know how to do this?

    Comment

    • Bill Schanks

      #3
      Re: export only visible columns from datagridview to a text file

      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.OwningColu mn.Visible or
      cell.OwningColu mn.Width.

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

      On Jun 5, 10:52 am, TG <jtam...@yahoo. comwrote:
      On Jun 3, 4:18 pm, TG <jtam...@yahoo. comwrote:
      >
      >
      >
      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(B yVal sender As System.Object, ByVal e As
      System.EventArg s) Handles Button5.Click
      >
      Dim writer As StreamWriter = New StreamWriter("R :\Spam BB
      Search Engine Application\Exp orts\GridExport .txt")
      If (DataGridView1. Rows.Count 0) Then
      For Each col As DataGridViewCol umn In
      DataGridView1.C olumns
      If (col.Index = (DataGridView1. Columns.Count - 1))
      Then
      writer.WriteLin e(col.HeaderTex t)
      Else
      writer.Write(St ring.Concat(col .HeaderText, ","))
      End If
      Next
      For Each row As DataGridViewRow In DataGridView1.R ows
      'If Not omitIndices.Con tains(row.Index ) Then
      For Each cell As DataGridViewCel l In row.Cells
      If (cell.OwningCol umn.Index _
      = (DataGridView1. Columns.Count - 1))
      Then
      If (Not (cell.Value) Is Nothing) Then
      writer.WriteLin e(cell.Value.To String)
      Else
      writer.WriteLin e("")
      End If
      ElseIf (Not (cell.Value) Is Nothing) Then
      >
      writer.Write(St ring.Concat(cel l.Value.ToStrin g, ","))
      Else
      writer.Write(St ring.Concat("", ","))
      End If
      Next
      'End If
      Next
      End If
      writer.Close()
      >
      End Sub
      >
      does anybody know how to do this?

      Comment

      Working...