Public Class frmDepartment
    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        If txtDepartment.Text = "" Then
            ErrorProvider1.SetError(txtDepartment, "Department Name.")
        Else
            ErrorProvider1.Clear()
            If btnSubmit.Text = "SAVE" Then
                ExecuteSQLQuery("INSERT INTO Department (DepartmentName) VALUES ('" + str_repl(txtDepartment.Text) + "')")
            ElseIf btnSubmit.Text = "UPDATE" Then
                ExecuteSQLQuery("UPDATE  Department SET DepartmentName='" + str_repl(txtDepartment.Text) + "' WHERE DEP_ID=" & num_fltr(txtDepID.Text) & " ")
            End If
            LoadData()
            btnSubmit.Text = "SAVE"
            txtDepID.Text = ""
            txtDepartment.Text = ""
            MsgBox("Information has been saved successfully.", MsgBoxStyle.Information, "Information")
        End If
    End Sub
    Private Sub frmDepartment_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub
    Private Sub LoadData()
        FillDataGrid("SELECT DEP_ID, DepartmentName FROM Department", DataGridView1)
    End Sub
    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If e.ColumnIndex = 0 Then
            If MsgBox("Are you sure to edit: " & DataGridView1.CurrentRow.Cells(3).Value, MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                txtDepID.Text = DataGridView1.CurrentRow.Cells(2).Value
                txtDepartment.Text = DataGridView1.CurrentRow.Cells(3).Value
                btnSubmit.Text = "UPDATE"
            End If
        ElseIf e.ColumnIndex = 1 Then
            If MsgBox("Are you sure to delete: " & DataGridView1.CurrentRow.Cells(3).Value, MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("DELETE FROM  Department  WHERE DEP_ID=" & num_fltr(DataGridView1.CurrentRow.Cells(2).Value) & " ")
                LoadData()
                MsgBox("Data has been deleted successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub
End Class