VB.Net Import And Export Text File To DataGridView - Hallo sahabat Dev-Create, Pada Artikel yang anda baca kali ini dengan judul VB.Net Import And Export Text File To DataGridView, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel dataGridView, Artikel datagridview vb.net, Artikel Export Datagridview Data To Text File In Visual Basic.Net, Artikel Import Text File Text To DataGridview In VB.Net, Artikel vb.net, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : VB.Net Import And Export Text File To DataGridView
link : VB.Net Import And Export Text File To DataGridView

Baca juga


VB.Net Import And Export Text File To DataGridView

How To Get And Set DataGridView Data To Txt File Text Using Visual Basic .Net

datagridview import and export to a text file in vb.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.





▶ Download All VB.NET Projects Source Code


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:

import and export txt file text to datagridview using vb.net







Demikianlah Artikel VB.Net Import And Export Text File To DataGridView

Sekianlah artikel VB.Net Import And Export Text File To DataGridView kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel VB.Net Import And Export Text File To DataGridView dengan alamat link https://dev-create.blogspot.com/2021/11/vbnet-import-and-export-text-file-to.html