473,396 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

export only visible columns from datagridview to a text file

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
Jun 27 '08 #1
2 3903
TG
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(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?
Jun 27 '08 #2
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 <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(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?
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Shawn Mehaffie | last post by:
I have the following class that I've wirtten to take a Dataset and automatically export it to either XML, ASCII or Tab delimited file. The reason I wrote it they way I did was that I don't want to...
5
by: Jonny | last post by:
Hello, I have created a button on my form which when pressed does the following : 1) Run a pre-defined Macro, which is picking up a query and running my data to excel. However, I need the...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
0
by: artteam | last post by:
Hi All, I try to export the table in DataGridView control on my form to MS excel with the following code. private void fnExtractToExcel() { ...
5
by: Mike Wilson | last post by:
Hello all, I'd like to export a DataGridView to Excel. I am using .NET 2.0 and VB.NET. But I don't know if the user has Excel on their machines, so can't use COM Excel object. Any ideas...
3
by: jmarcrum | last post by:
I want to export a report (that contains two separate queries, 1. Current year data, and 2. split-year data) from access into excel, but everytime I run my code and export the data to excel, it looks...
2
by: TG | last post by:
Hi! I am trying to export only the visible columns from a datagridview in my windows form in VB 2008. Should't it be no comma after the first row where the headers are? Also should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.