|
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
On Jun 3, 4:18 pm, TG wrote:
> 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?
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.
On Jun 5, 10:52 am, TG wrote:
Click to show or hide original message or reply text.
> On Jun 3, 4:18 pm, TG wrote:
>
>
>
> > 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?
|