Advertisement
stevennathaniel

Excel: Menampilkan Data dari Datagridview ke TextBox

Feb 17th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.63 KB | None | 0 0
  1. Imports System.Data.OleDb
  2.  
  3. Imports System.Data
  4.  
  5.  
  6. Public Class Form6
  7.  
  8.     Dim Nama As String
  9.  
  10.     Dim Alamat As String
  11.  
  12.     Dim oledbKoneksi As OleDbConnection = New OleDbConnection
  13.  
  14.     Dim oledbPerintah As OleDbCommand = New OleDbCommand
  15.  
  16.     Dim lokasiFile As String = "D:\fileExcel\"
  17.  
  18.     Dim namaFile As String = "LatihanOLEDB2.xlsx"
  19.  
  20.     Dim stringKoneksi As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & lokasiFile & namaFile & ";" & "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
  21.  
  22.     Dim adapter As New OleDbDataAdapter
  23.  
  24.  
  25.     Sub tampilIsiGrid()
  26.  
  27.         oledbKoneksi.ConnectionString = stringKoneksi
  28.  
  29.         oledbKoneksi.Open()
  30.  
  31.         oledbPerintah.Connection = oledbKoneksi
  32.  
  33.         oledbPerintah.CommandText = "SELECT * FROM [DataKaryawan$]"
  34.  
  35.         adapter.SelectCommand = oledbPerintah
  36.  
  37.  
  38.         Dim ds As DataSet
  39.  
  40.         ds = New DataSet
  41.  
  42.         adapter.Fill(ds)
  43.  
  44.         DataGridView1.DataSource = ds.Tables(0)
  45.  
  46.         oledbKoneksi.Close()
  47.  
  48.  
  49.         'MsgBox("Done!")
  50.  
  51.  
  52.     End Sub
  53.  
  54.  
  55.  
  56.  
  57.  
  58.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  59.  
  60.  
  61.         'oledbKoneksi.ConnectionString = stringKoneksi
  62.  
  63.         'oledbKoneksi.Open()
  64.  
  65.         'oledbPerintah.Connection = oledbKoneksi
  66.  
  67.         'oledbPerintah.CommandText = "SELECT * FROM [DataKaryawan$]"
  68.  
  69.         'adapter.SelectCommand = oledbPerintah
  70.  
  71.  
  72.         'Dim ds As DataSet
  73.  
  74.         'ds = New DataSet
  75.  
  76.         'adapter.Fill(ds)
  77.  
  78.         'DataGridView1.DataSource = ds.Tables(0)
  79.  
  80.         'MsgBox("Done!")
  81.  
  82.  
  83.         Nama = TextBox1.Text
  84.  
  85.         Alamat = TextBox2.Text
  86.  
  87.         oledbKoneksi.ConnectionString = stringKoneksi
  88.  
  89.         oledbKoneksi.Open()
  90.  
  91.         oledbPerintah.Connection = oledbKoneksi
  92.  
  93.         oledbPerintah.CommandText = "INSERT INTO DataKaryawan(Nama, Alamat) values " & "('" & Nama & "', '" & Alamat & "')"
  94.  
  95.         oledbPerintah.ExecuteNonQuery()
  96.  
  97.         oledbKoneksi.Close()
  98.  
  99.         TextBox1.Text = ""
  100.  
  101.         TextBox2.Text = ""
  102.  
  103.         DataGridView1.Refresh()
  104.  
  105.         tampilIsiGrid()
  106.  
  107.  
  108.         'tampilIsiGrid()
  109.  
  110.  
  111.     End Sub
  112.  
  113.     Private Sub Form6_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  114.  
  115.         tampilIsiGrid()
  116.  
  117.  
  118.     End Sub
  119.  
  120.     Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  121.  
  122.         Dim row As DataGridViewRow = DataGridView1.CurrentRow
  123.  
  124.         Me.TextBox1.Text = row.Cells(0).Value.ToString
  125.  
  126.         Me.TextBox2.Text = row.Cells(1).Value.ToString
  127.  
  128.  
  129.  
  130.     End Sub
  131. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement