Nanda's Blog

Share a smile, Share information

Memasukkan data dari file Txt ke Datagridview pada Visual Basic 2008 February 23, 2011

Filed under: Pemrograman,Visual Basic 2008 — nanda @ 7:36 pm
Tags:

Bagaimana memasukkan data yang berasal dari file txt ke dalam datragridview pada visual basic 2008?
Saya coba-coba buat script nya, silakan kalo mw dicoba, mudah-mudahan bisa membantu ya..๐Ÿ™‚

'saat button ok di klik
Private Sub button_ok_text_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles button_ok_text.Click
'mengambil isi file txt yang di load
Dim Arr(4) As String
'datagridview_text = nama datagridview
datagridview_text.Rows.Clear()
datagridview_text.Columns.Clear()
datagridview_text.AlternatingRowsDefaultCellStyle.BackColor = Color.Silver

Try
 Dim LineIn As String
 Dim split_array() As String
 Dim value As Integer = -1
 Dim oFile As System.IO.File 'VARIABEL UTK MEMBUKA CLASS I/O
 Dim oRead As System.IO.StreamReader 'VARIABEL UTK MEMBUKA CLASS I/O UNTUK MEMBACA
 Dim awal As Boolean
 Dim field_delimiter As String
 field_delimiter = combobox_field_delimiter.Text 
 'combobox_field_delimiter.Text= pemilihan delimiter berisi jenis2 delimiter --> , . | ; /

 'MEMBACA DIREKTORY FILE TXT YG DIPAKAI
 oRead = oFile.OpenText(path:=textbox_open_text.Text)
 'PERULANGAN UNTUK MEMBACA BARIS PER BARIS DG ENTER SEBAGAI PENANDA ROW NYA
 awal = True
 While oRead.Peek <> -1
   LineIn = oRead.ReadLine()
   split_array = Split(LineIn, field_delimiter) 'SPLIT TEXT BERDASARKAN DELIMETER
   For i As Integer = 0 To split_array.Length = -1
     If awal = True Then
       For k As Integer = 0 To split_array.Length - 1
         datagridview_text.Columns.Add(split_array(k), split_array(k))
         awal = False
       Next
     Else
       'MEMASUKKAN VALUE YG DIDAPAT DARI TXT FILE KE LIST GRID VIEW
       datagridview_text.Rows.Add(split_array)
     End If
   Next
 End While
 oRead.Close() 'KONEKSI KE FILE TEXT DIPUTUS
Catch 'AKAN TAMPIL PESAN ERROR PADA SAAT TERJADI KESALAHAN
 MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Try
 

4 Responses to “Memasukkan data dari file Txt ke Datagridview pada Visual Basic 2008”

  1. made Says:

    hai …. bisa tukar info tentang visual studio.net
    gw ada di http://catatan-made.co.cc/

  2. Ade Says:

    delimeter ga berguna sis.. aq jalanin dia ga bisa. combobox_field_delimiter.Text= “/”

  3. silvester Says:

    apa ada tutorila videonya ngak ? maaf saya masi pemula nih. kebetulan yang saya cari tips seperti ini., di google satu”nya cuman nandasblog yang saya dapat .tx


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s