Option Strict Off
Imports System.Data.Odbc
Public Class frmPenilai

    Sub databaru()
        txtNama.Text = ""
        txtJabatan.Text = ""
        cboStatus.Text = ""
        txtnama.Focus()
    End Sub
    Sub kosongkan()
        Call tidaksiapisi()
        txtIdPenilai.Text = ""
        txtnama.Text = ""
        txtJabatan.Text = ""
        cboStatus.Text = ""
        txtIdPenilai.Enabled = True
        txtIdPenilai.Focus()
    End Sub
    Sub siapisi()
        txtnama.Enabled = True
        txtJabatan.Enabled = True
        cboStatus.Enabled = True
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        btnSimpan.Enabled = True
    End Sub
    Sub tidaksiapisi()
        txtnama.Enabled = False
        txtJabatan.Enabled = False
        cboStatus.Enabled = False
        btnSimpan.Enabled = False
        btnHapus.Enabled = False
        btnEdit.Enabled = False
    End Sub
    Sub CekData()
        cmd = New OdbcCommand("select * from tbpejabat where id_Pejabat='" & txtIdPenilai.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.HasRows = True Then
            txtnama.Text = rd.Item(1)
            txtJabatan.Text = rd.Item(2)
            cboStatus.Text = rd.Item(3)
            Call tidaksiapisi()
            txtIdPenilai.Enabled = False
            btnEdit.Enabled = True
            btnHapus.Enabled = True
            btnSimpan.Enabled = False
        Else
            Call databaru()
            Call siapisi()
            btnSimpan.Enabled = True
        End If
    End Sub

    Sub Tampilkan()
        da = New OdbcDataAdapter("Select * from tbpejabat order by Id_pejabat", Conn)
        ds = New DataSet
        ds.Clear()
        da.Fill(ds, "tbpejabat")
        DGV.DataSource = (ds.Tables("tbpejabat"))
        DGV.ReadOnly = True
        DGV.Columns(0).Width = 100
        DGV.Columns(1).Width = 170
        DGV.GridColor = Color.Blue
        DGV.DefaultCellStyle.ForeColor = Color.Blue
        DGV.RowHeadersDefaultCellStyle.BackColor = Color.DeepPink
        DGV.AlternatingRowsDefaultCellStyle.BackColor = Color.Yellow
        DGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DGV.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        DGV.Columns(0).HeaderText = "Id. Penilai"
        DGV.Columns(1).HeaderText = "Nama "
        DGV.Columns(2).HeaderText = "Jabatan"

    End Sub





    Private Sub txtuserId_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIdPenilai.KeyPress
        If e.KeyChar = Chr(13) Then
            Call CekData()

        End If
    End Sub

    Private Sub txtIdPenilai_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtIdPenilai.Validated
        btnSimpan.Enabled = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        btnEdit.Enabled = False
        btnHapus.Enabled = False
        Call siapisi()
        txtnama.Focus()
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        If txtIdPenilai.Text = "" Or txtnama.Text = "" Or txtJabatan.Text = "" Or cboStatus.Text = "" Then
            MsgBox("Data Belum Lengkap", , "Perhatian...!")
            txtIdPenilai.Focus()
            Exit Sub
        End If
        cmd = New OdbcCommand("Select * from tbpejabat where id_Pejabat='" & txtIdPenilai.Text & "'", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If Not rd.HasRows Then
            Dim sqltambah As String = "Insert into tbpejabat (id_Pejabat,nama,jabatan,status) values " & _
            "('" & txtIdPenilai.Text & "','" & txtnama.Text & "','" & (txtJabatan.Text) & "','" & cboStatus.Text & "')"
            cmd = New OdbcCommand(sqltambah, Conn)

        Else
            Dim sqledit As String = "Update tbpejabat set " & _
            "nama='" & txtnama.Text & "',jabatan='" & txtJabatan.Text & "', " & _
            "status='" & cboStatus.Text & "' where id_pejabat='" & txtIdPenilai.Text & "'"
            cmd = New OdbcCommand(sqledit, Conn)

        End If
        cmd.ExecuteNonQuery()
        Call Tampilkan()
        Call kosongkan()
        btnSimpan.Enabled = False
    End Sub

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

    Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
        cmd = New OdbcCommand("Select count(*) as jml from tbpejabat ", Conn)
        rd = cmd.ExecuteReader
        rd.Read()
        If rd.Item(0) = 1 Then
            MsgBox("Maaf..., Data User tidak dapat dihapus", MsgBoxStyle.Critical, "Perhatian...!")
            Exit Sub
        Else
            If MessageBox.Show("Yakin akan dihapus..?", "Hapus Data", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                cmd = New OdbcCommand("Delete  from tbpejabat where id_pejabat='" & txtIdPenilai.Text & "'", Conn)
                cmd.ExecuteNonQuery()

            End If
        End If
        Call Tampilkan()
        Call kosongkan()
    End Sub

    Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
        Call kosongkan()
    End Sub

    Private Sub txtnama_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtnama.TextChanged
        txtnama.Text = StripNonAlphaNum(txtnama.Text)
    End Sub

    Private Sub frmPenilai_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub frmPenilai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call Koneksi()
        Call Tampilkan()
        Call databaru()
        Call tidaksiapisi()
        Call kosongkan()
    End Sub

   
    Private Sub DGV_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick

    End Sub

    Private Sub DGV_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellDoubleClick
        txtIdPenilai.Text = DGV.SelectedRows.Item(0).Cells(0).Value()
        Call CekData()
    End Sub

    Private Sub txtIdPenilai_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIdPenilai.TextChanged

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class