How To Get And Set DataGridView Data To Txt File Text Using Visual Basic .Net
In this VB.NET Tutorial we will see How To Import Records From A Text File And Display The Values Into DataGridView, and Export DataGridView Rows Data To a Txt File Using DataTable, TextWriter, StreamWriter, ReadAllLines In Visual Basic.Net   Programming Language And Visual  Studio Editor.
Project Source Code:
Imports System.IO
Public Class TXT_IMPORT_EXPORT_DGV
    Dim table1 As New DataTable()
    Dim table2 As New DataTable()
    Private Sub TXT_IMPORT_EXPORT_DGV_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' add columns to your datatable
        ' with the name of the columns and their type
        table1.Columns.Add("Id", Type.GetType("System.Int32"))
        table1.Columns.Add("First Name", Type.GetType("System.String"))
        table1.Columns.Add("Last Name", Type.GetType("System.String"))
        table1.Columns.Add("Age", Type.GetType("System.Int32"))
        ' add rows to datatable with some data
        table1.Rows.Add(1, "AAAA", "BBBB", 21)
        table1.Rows.Add(2, "CCCC", "DDDD", 33)
        table1.Rows.Add(3, "EEEE", "FFFF", 53)
        table1.Rows.Add(4, "GGGG", "HHHH", 19)
        table1.Rows.Add(5, "MMMM", "NNNN", 36)
        table1.Rows.Add(6, "RRRR", "SSSS", 63)
        ' now set the datagridview datasource equals to your datatable name
        DataGridView1.DataSource = table1
        table2.Columns.Add("Id", Type.GetType("System.Int32"))
        table2.Columns.Add("First Name", Type.GetType("System.String"))
        table2.Columns.Add("Last Name", Type.GetType("System.String"))
        table2.Columns.Add("Age", Type.GetType("System.Int32"))
        DataGridView2.DataSource = table2
    End Sub
' button export
    Private Sub ButtonExport_Click(sender As Object, e As EventArgs) Handles ButtonExport.Click
        Dim writer As New StreamWriter("C:\Users\1BestCsharp\Desktop\Table2.txt")
        For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
            For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
                ' if last column
                If j = DataGridView1.Columns.Count - 1 Then
                    writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString())
                Else
                    writer.Write(vbTab & DataGridView1.Rows(i).Cells(j).Value.ToString() & vbTab & "|")
                End If
            Next j
            writer.WriteLine("")
        Next i
        writer.Close()
        MessageBox.Show("Data Exported")
    End Sub
    ' button import
    Private Sub ButtonImport_Click(sender As Object, e As EventArgs) Handles ButtonImport.Click
        Dim lines() As String
        Dim vals() As String
        lines = File.ReadAllLines("C:\Users\1BestCsharp\Desktop\table2.txt")
        For i As Integer = 0 To lines.Length - 1 Step +1
            vals = lines(i).ToString().Split("|")
            Dim row(vals.Length - 1) As String
            For j As Integer = 0 To vals.Length - 1 Step +1
                row(j) = vals(j).Trim()
            Next j
            table2.Rows.Add(row)
        Next i
    End Sub
End Class
OutPut:
Download Projects Source Code
    
  
  
  
