Public Class frmListOfAvailedLeave1
    Private Sub LoadData()
        FillDataComboBox("SELECT   EMPLOYEE_ID, EMP_CODE  FROM   Employee_Information  ORDER BY EMP_CODE", "EMPLOYEE_ID", "EMP_CODE", cmbEmployee)
        FillDataGrid(" SELECT  Leave1.LEVE_ENROLL_ID, Employee_Information.EMP_CODE, Leave1.LeaveType, Leave1.StartDate, Leave1.EndDate, Leave1.NumberOfDays, Leave1.Reason " &
                     " FROM    Leave1 LEFT OUTER JOIN Employee_Information ON Leave1.EMPLOYEE_ID = Employee_Information.EMPLOYEE_ID ", DataGridView1)
    End Sub

    Private Sub frmListOfAvailedLeave_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadData()
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        LoadData()
    End Sub

    Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        If rbFilterbyEmployeeAnDate.Checked = True Then
            If cmbEmployee.SelectedValue Is Nothing Or cmbEmployee.SelectedIndex = -1 Then
                ErrorProvider1.SetError(cmbEmployee, "Mandatory")
            Else
                ErrorProvider1.Clear()
                FillDataGrid(" SELECT  Leave1.LEVE_ENROLL_ID, Employee_Information.EMP_CODE, Leave1.LeaveType, Leave1.StartDate, Leave1.EndDate, Leave1.NumberOfDays, Leave1.Reason " &
                             " FROM    Leave1 LEFT OUTER JOIN Employee_Information ON Leave1.EMPLOYEE_ID = Employee_Information.EMPLOYEE_ID " &
                             " WHERE   (Leave1.StartDate >= '" & Format(dtpFrom.Value, "yyyy-MM-dd") & "') AND (Leave1.EndDate <= '" & Format(dtpTo.Value, "yyyy-MM-dd") & "') AND (Leave1.EMPLOYEE_ID = " & num_fltr(cmbEmployee.SelectedValue) & ") ", DataGridView1)
            End If
        Else
            FillDataGrid(" SELECT  Leave1.LEVE_ENROLL_ID, Employee_Information.EMP_CODE, Leave1.LeaveType, Leave1.StartDate, Leave1.EndDate, Leave1.NumberOfDays, Leave1.Reason " &
                         " FROM    Leave1 LEFT OUTER JOIN Employee_Information ON Leave1.EMPLOYEE_ID = Employee_Information.EMPLOYEE_ID " &
                         " WHERE   (Leave1.StartDate >= '" & Format(dtpFrom.Value, "yyyy-MM-dd") & "') AND (Leave1.EndDate <= '" & Format(dtpTo.Value, "yyyy-MM-dd") & "') ", DataGridView1)
        End If
    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 you want to edit this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                With frmLeave
                    .MdiParent = frmMDIParent1
                    .Show()
                    .txtLEVE_ENROLL_ID.Text = DataGridView1.CurrentRow.Cells(2).Value

                    ExecuteSQLQuery(" SELECT     Leave1.LEVE_ENROLL_ID, Employee_Information.EMP_CODE, Leave1.LeaveType, Leave1.StartDate, Leave1.EndDate, Leave1.Reason " &
                                    " FROM  Leave1 LEFT OUTER JOIN  Employee_Information ON Leave1.EMPLOYEE_ID = Employee_Information.EMPLOYEE_ID  WHERE    (Leave1.LEVE_ENROLL_ID = " & DataGridView1.CurrentRow.Cells(2).Value & ") ")
                    If sqlDT.Rows.Count > 0 Then
                        .txtEmployeesCode.Text = sqlDT.Rows(0)("EMP_CODE")
                        .txtReason.Text = sqlDT.Rows(0)("Reason")
                        .cmbLeaveType.Text = sqlDT.Rows(0)("LeaveType")
                        .dtpStartDate.Value = sqlDT.Rows(0)("StartDate")
                        .dtpEndDate.Value = sqlDT.Rows(0)("EndDate")
                    End If
                    .btnSearch.PerformClick()
                    .btnSubmit.Text = "UPDATE"
                End With
            End If
        ElseIf e.ColumnIndex = 1 Then
            If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Confirm..") = MsgBoxResult.Yes Then
                ExecuteSQLQuery("DELETE FROM Leave1 WHERE  LEVE_ENROLL_ID =" & DataGridView1.CurrentRow.Cells(2).Value & " ")
                LoadData()
                MsgBox("Record deleted successfully.", MsgBoxStyle.Information, "Information")
            End If
        End If
    End Sub
End Class