Breaking News

Main Menu

Export Datagridview To Text File Vb.net

среда 15 апреля admin 76
Export Datagridview To Text File Vb.net Rating: 5,8/10 3898 votes
P: 5
Hi, I was working with CSV file and DataGridView in VB.NET. Having lots fo complexities i succeeded to retrieve a 5 columns CSV file into DataGridView. It is to be mentioned that the last two columns in the dgv are Combo type.
My goal was after retrieving the csv file I will edit other two selection columns and export the modified file in a different locations.
the content of the CSV file is as follows:
2014001,'Mensch, Samantha',CD Models Christian values,S,*
2014001,'Mensch, Samantha',CD Shows reverence during pray,S,
2014001,'Mensch, Samantha',CD Accepts responsibility for,S,
2014001,'Mensch, Samantha',CD Practices self control,S,
2014001,'Mensch, Samantha',CD Observes school rules,S,*
2014001,'Mensch, Samantha',CD Respects property,S,
2014001,'Mensch, Samantha',CD Respects others,S,
2014001,'Mensch, Samantha',Uses time wisely,S,
2014001,'Mensch, Samantha',Displays effort,S,
2014001,'Mensch, Samantha',Works well in a group,S,
2014001,'Mensch, Samantha',Works well independently,S,
Code for the Reading CSV file as follows under a button:
Sub Action_Button
Dim lineNumber As Integer = 0
Dim line As String
Dim lToken() As String
Dim sTemp As String
Dim sGridRow(4) As String
OpenFileDialog1.InitialDirectory = 'c:temp'
'openFileDialog1.Filter = 'All files (*.*) *.*'
OpenFileDialog1.Filter = 'CSV files (*.csv) *.CSV'
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
fName = OpenFileDialog1.FileName
Me.Text = fName
End If
Me.Text = fName
Try
Dim reader As IO.StreamReader = New IO.StreamReader(fName)
'DataGridView2.Rows.Remove()
DataGridView2.Rows.Clear()
While reader.Peek <> -1
line = reader.ReadLine()
lToken = Split(line, ',')
For i As Integer = 0 To lToken.Length - 3
sTemp = Replace(lToken(i), '', ')
sGridRow(i) = sTemp
Next
Me.DataGridView2.Rows.Add(sGridRow)
lineNumber += 1
End While
'Me.Text = Str(lineNumber)
reader.Close()
Catch ex As Exception
MessageBox.Show('Error occured while reading the csv file.' + ex.ToString)
End Try
End Sub
The above code works fine, Please see the attached.
Now I face the problem after modifying the row in the dgv; suppose I have selected a grade from the combo. Now I want to export the file in a selected location as with the same name as I imported.
I can import the file taking the data from the dvg. I gave the code here to populate the dvg rows. I need entire dvg data to read and write them in the file.
The code I don know..How to take the data from the dbg from the first row to the last? The code I wrote is pasted here, but it simply populates the current row column.
Private Sub SaveGridDataInFile(ByRef fName As String)
'Dim writer As IO.StreamWriter = New IO.StreamWriter(fName)
Dim I As Integer = 0
Dim j As Integer = 0
Dim cellvalue$
Dim rowLine As String = '
Try
FileOpen(1, fName, OpenMode.Output) ' Open file for output.
'WriteLine(1, 'Hello', ' ', 'World') ' Separate strings with space.
'WriteLine(1, SPC(5), '5 leading spaces ') ' Print five leading spaces.
'WriteLine(1, TAB(10), 'Hello') ' Print word at column 10.
'FileClose(1) ' Close file.
'sData = DataGridView1.Columns(1).Name.ToString()
'sData = Me.DataGridView1.CurrentRow.Cells(i).Value.ToStrin g
'MessageBox.Show(sData)
For j = 0 To (DataGridView2.Rows.Count - 2)
For I = 0 To (DataGridView2.Columns.Count - 1)
If Not TypeOf DataGridView2.CurrentRow.Cells.Item(I).Value Is DBNull Then
cellvalue = DataGridView2.CurrentRow.Cells.Item(I).Value.ToStr ing
Else
cellvalue = '
End If
'DataGridView2.CurrentRow.Cells.Item(I).Value = cellvalue
rowLine = rowLine + cellvalue + ','
Next
WriteLine(1, TAB(0), rowLine) ' Print word at column 10.
'writer.WriteLine()
rowLine = '
Next
FileClose(1) ' Close file.
Catch e As Exception
MessageBox.Show('Error occured while writing to the file.' + e.ToString())
Finally
FileClose(1)
End Try
End Sub
Could you please help me giving the code that reads entire row column of the dvg?

VB.NET - export datagridview to text file - Asked By Rahul Kumar on 20-Feb-12 05:20 AM. Fallout 4 legendary railway rifle price. I am trying to export data from datagridview to the text file, for that my.