TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Rosa Vila
NA
12
2.3k
Export Datagridview to text file
Mar 13 2015 12:57 PM
I have a small program that helps me to import excel sheets to a Datagridview to later export it to a text file but the problem is that the text file is overwritten. I would like that every time you export the contents of datagridview a new file is created with a different name to avoid overwriting it . I would be very grateful for his help. I use the following code in a button to export the datagrid to text file:
If DataGridView1.RowCount = 0 Then
MessageBox.Show("the datagridview is emptly")
Else
If Directory.Exists("C:\Foldertxt") = False Then
Directory.CreateDirectory("C:\Foldertxt")
End If
Dim sFile As String = "C:\Foldertxt\file.txt"
If File.Exists(sFile) = True Then
My.Computer.FileSystem.DeleteFile(sFile, FileIO.UIOption.OnlyErrorDialogs, _
FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
End If
Using f As New IO.StreamWriter(sFile, True)
Dim col As String = ""
Dim row As String = ""
Dim i As Integer = 0
For Each r As DataGridViewRow In DataGridView1.Rows
For Each c As DataGridViewColumn In DataGridView1.Columns
row = row & "'" & Convert.ToString(r.Cells(c.HeaderText).Value) & "' "
Next
If i < DataGridView1.Rows.Count - 1 Then row &= Environment.NewLine
Next
f.WriteLine(row)
MessageBox.Show("file created")
End Using
End If
I use Visual Studio 2013
Thank so much and sorry for my english
Reply
Answers (
2
)
Winform objects change their Font after focus
SQL Server Express connection string