Jumat, 07 Oktober 2011

Membuat Form (User Interface)

Membuat Form (User Interface)               
Tarik dua label dan 2 textbox, 2 RadioButton, 1 datagridview dan 6 command button dari toolbox ke form.

Jika anda melakukannya dengan benar seharusnya  form nya seperti ini:
 
Aplikasi ini telah dites dan berjalan dengan mulus.
Sebagai bocoran saya pernah tes pada perusahaan software developer. Tes nya adalah membuat software seperti ini ;)


Inilah kode lengkapnya:
Imports System.Data.SqlClient
Imports System.Data

Public Class frmKota

Private oConn As New SqlConnection
Private sConn As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Dataku;Integrated Security=True"

Private m_pos As Integer
Private oTbl As New DataTable

Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
  Dim oDS As New DataSet
  Dim oDa As New SqlDataAdapter("SELECT * FROM KOTA", sConn)
  oDa.Fill(oDS) oTbl = oDS.Tables(0)
  DataGridView1.DataSource = oTbl
End Sub

Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
  End
End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged

  m_pos = BindingContext(oTbl).Position
  txtKodeKota.Text = oTbl.Rows(m_pos).Item(0)
  txtNamaKota.Text = oTbl.Rows(m_pos).Item(1)
End Sub

Private Sub cmdTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTambah.Click
 
  Dim sql As String = "INSERT INTO KOTA VALUES ('" & txtKodeKota.Text & "','" & txtNamaKota.Text & "')"

  Dim oCmd As New SqlCommand
  oCmd.Connection = oConn
  oCmd.CommandText = sql
  oCmd.ExecuteNonQuery()
End Sub

Private Sub frmKota_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  oConn.ConnectionString = sConn
  oConn.Open()

End Sub


Private Sub cmdCari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCari.Click

  Dim oDS As New DataSet
  Dim oDa As New SqlDataAdapter("SELECT * FROM KOTA WHERE KODE_KOTA='" & txtKodeKota.Text & "'", sConn)
  oDa.Fill(oDS) oTbl = oDS.Tables(0)
  DataGridView1.DataSource = oTbl

End Sub


Private Sub cmdHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHapus.Click

  Dim sql As String = "DELETE FROM KOTA WHERE KODE_KOTA='" & txtKodeKota.Text & "'"
 
  Dim oCmd As New SqlCommand
  oCmd.Connection = oConn
  oCmd.CommandText = sql
  oCmd.ExecuteNonQuery()

End Sub


Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click

  Dim sql As String = "UPDATE KOTA SET NAMA_KOTA='" & txtNamaKota.Text & "' WHERE KODE_KOTA='" & txtKodeKota.Text & "'"

  Dim oCmd As New SqlCommand
  oCmd.Connection = oConn
  oCmd.CommandText = sql
  oCmd.ExecuteNonQuery()

End Sub


Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

  Dim oDS As New DataSet
  Dim oDa As New SqlDataAdapter("SELECT * FROM KOTA ORDER BY KODE_KOTA ASC", sConn)

  oDa.Fill(oDS) oTbl = oDS.Tables(0)
  DataGridView1.DataSource = oTbl

End Sub


Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

  Dim oDS As New DataSet
  Dim oDa As New SqlDataAdapter("SELECT * FROM KOTA ORDER BY NAMA_KOTA ASC", sConn)

  oDa.Fill(oDS) oTbl = oDS.Tables(0)
  DataGridView1.DataSource = oTbl
End Sub

End Class


Ini adalah salah satu aksi software yang baru saja kita buat.






Catatan:

Jika Anda memakai database MS-Access ikuti petunjuk di bawah ini:

Imports System.Data.SqlClient -> Imports System.Data.OleDb

Private sConn As String = "Data Source=.\SQLEXPRESS;Initial Catalog=Dataku;Integrated Security=True"
->
Private sConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dataku.mdb"

Dim oCmd As New SqlCommand -> Dim oCmd As New OleDbCommand


-> maksudnya ganti menjadi...

Tidak ada komentar:

Posting Komentar