Thursday, June 14, 2012

Menampilkan data pada datagridview dan textbox

Saat ini saya mencoba-coba bagaimana menampilkan datagridview dan textbox pada Visual Basic.Net, sebelumnya mari buat terlebih dahulu pada database dblat dengan nama tabel barang seperti gambar dibawah ini :


Seperti Biasa pada program aplikasi Visual Basic.Net, pada menubar terlihat nama project. Klik project>>Add Reference. Klik Mysql.Data>>Ok



Berikut adalah tampilan Form Data Barang Beserta Codingnya.

Imports MySql.Data.MySqlClient

Public Class Form1
    Dim con As New MySqlConnection(strkon)
    Dim perintah As New MySqlCommand
    Dim cek As MySqlDataReader
    Dim data As New MySqlDataAdapter
    Dim aktn As Integer
    Dim simpan As Boolean


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        displaymhs("select * from barang order by IDBarang asc limit 10")
    End Sub

    Sub displaymhs(ByVal sql As String) ' untuk menampilkan data pada datagrid view
        con.Open()
        perintah.Connection = con
        perintah.CommandType = CommandType.Text
        perintah.CommandText = sql
        data.SelectCommand = perintah
        Dim datas As New DataSet
        data.Fill(datas, "IDBarang")
        DataGridView1.DataSource = datas.Tables("IDBarang")
        con.Close()
    End Sub

    Sub aktiftextbox()
        TextBox1.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
        TextBox4.Enabled = True
    End Sub
    Sub tidakaktiftextbox()
        TextBox1.Enabled = False
        TextBox2.Enabled = False
        TextBox3.Enabled = False
        TextBox4.Enabled = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        aktiftextbox()
        TextBox1.Focus()
        clearinput()
        Button2.Enabled = True
        Button3.Enabled = True
        Button4.Enabled = True
        Button5.Enabled = True
        simpan = True
    End Sub

   '-----------------------------datagridview--------------------------
    'menampilkan data ketika datadridview di klik pada cell
'----------------------------------------------------------------------
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim i As Integer
        i = Me.DataGridView1.CurrentRow.Index
        With DataGridView1.Rows.Item(i)
            Me.TextBox1.Text = .Cells(0).Value
            Me.TextBox2.Text = .Cells(1).Value
            Me.TextBox3.Text = .Cells(2).Value
            Me.TextBox4.Text = .Cells(3).Value
        End With
    End Sub

    'menampilkan data ketika konten /isi datagridview di klik tanda panah pada datagrid
'---------------------------------------------------------------------------------------
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        con.Open()
        perintah.Connection = con
        perintah.CommandType = CommandType.Text
        perintah.CommandText = "select * from barang"
        data.SelectCommand = perintah
        Dim datas As New DataSet
        data.Fill(datas, "IDBarang")
        DataGridView1.DataSource = datas.Tables("IDBarang")
        con.Close()
        Dim i As Integer
        i = Me.DataGridView1.CurrentRow.Index
        With DataGridView1.Rows.Item(i)
            Me.TextBox1.Text = .Cells(0).Value
            Me.TextBox2.Text = .Cells(1).Value
            Me.TextBox3.Text = .Cells(2).Value
            Me.TextBox4.Text = .Cells(3).Value
        End With
    End Sub

    'menampilkan data ketika datagridview di enter
'--------------------------------------------------
    Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
        Dim i As Integer
        Select Case e.KeyCode
            Case Keys.Enter
                i = Me.DataGridView1.CurrentRow.Index
                With DataGridView1.Rows.Item(i)
                    Me.TextBox1.Text = .Cells(0).Value
                    Me.TextBox2.Text = .Cells(1).Value
                    Me.TextBox3.Text = .Cells(2).Value
                    Me.TextBox4.Text = .Cells(3).Value
                End With
        End Select
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()
    End Sub

End Class

Semoga Bermanfaat.

0 komentar:

Post a Comment