VB.NET - export datagridview to text file - Asked By Rahul Kumar on 20-Feb-12 05:20 AM

i am trying to export data from datagridview to the text file, for that my code is

Dim numCols As Integer = DataGridView1.ColumnCount

Dim numRows As Integer = DataGridView1.RowCount - 1

Dim strDestinationFile As String = "d:\\output.txt"

Dim tw As TextWriter = New StreamWriter(strDestinationFile)

'writing the header

For count As Integer = 0 To numCols - 1

tw.Write(DataGridView1.Columns(count).HeaderText)

If (count <> numCols - 1) Then

tw.Write(

", ")

End If

Next

tw.WriteLine()

For count As Integer = 0 To numRows - 1

For count2 As Integer = 0 To numCols - 1

tw.Write(DataGridView1.Rows(count).Cells(count2).Value)

If (count2 <> numCols) Then

tw.Write(

", ")

End If

Next

tw.WriteLine()

Next

tw.Close()

MsgBox(

"data exported to the textfile succsessfully")

but in that case whenever i am exporting data it always store it in a "d:\\output.txt"  location
 so i can make this location as automatically generated location


dipa ahuja replied to Rahul Kumar on 20-Feb-12 05:31 AM
void Export()
{
FileStream fileStream = new FileStream(@"F:\file11.txt", FileMode.Create);
 
TextWriter sw = new StreamWriter(@"F:\\file11.txt");
int rowcount = dataGridView1.Rows.Count;
for (int i = 0; i < rowcount - 1; i++)
{
  sw.WriteLine(dataGridView1.Rows[i].Cells[0].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "\t" + dataGridView1.Rows[i].Cells[2].Value.ToString());
 }
   sw.Close();   //Don't Forget Close the TextWriter Object(sw)
 
   MessageBox.Show("Data Successfully Exported");
}
kalpana aparnathi replied to Rahul Kumar on 20-Feb-12 05:41 AM
hi,

Dim numCols As DataGridViewCell
        Dim sw As New System.IO.StreamWriter("d:\\output.txt")
        For Each numRows As DataGridViewRow In DataGridView1.Rows
            Dim intCellCount As Integer = numRows .Cells.Count
            Dim intCounter As Integer = 1
            For Each numCols  In numRows .Cells()
                If intCounter <> intCellCount Then
                    sw.Write(numCols .Value.ToString & "|")
                Else
                    sw.WriteLine(numCols .Value.ToString)
                End If
                intCounter += 1
            Next
        Next

Regards,
Somesh Yadav replied to Rahul Kumar on 20-Feb-12 05:58 AM
This code will export a datagrid into a text file line by line and separate ur columns by comma.

Private Sub BtnGentxt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGentxt.Click

Dim a, b As Integer

Dim Output As String

Dim numRows As Integer = dgCarton.BindingContext(dgCarton.DataSource, dgCarton.DataMember).Count

Const Seperator As String = ","

Dim FileName As String = "C:\carton.txt"
FileOpen(1, FileName, OpenMode.Output)

For a = 0 To numRows - 1

For b = 0 To 5 '5 is number of columns

If b = 5 Then

'Do not add the seperator at the end of the line.
Output = Output & CStr(dgCarton.Item(a, b))

Else
Output = Output & CStr(dgCarton.Item(a, b)) & Seperator

End If

Next
b = 0

Print(1, Output & vbNewLine)

Output = ""

Next
FileClose(1)



End Sub