Monday, 20 January 2014

Insert Grid Item & Change Background color of grid

Public Class Managecases
    Dim dataobject As New db
    Dim f As Integer
    Dim bl As New Bal
    Dim i As Integer
    Dim followdatedifference As String
    Public Sub gridformat()
        Gridcomplaints.Columns.Clear()
        Gridcomplaints.Rows.Clear()
        Gridcomplaints.EnableHeadersVisualStyles = False
        Gridcomplaints.Columns.Add("comp_no", "Complaint No")           '0
        Gridcomplaints.Columns.Add("dateandtime", "Last Communication") '1
        Gridcomplaints.Columns.Add("comptype", "Complaint Type")        '2
        Gridcomplaints.Columns.Add("Supervisor", "Supervisor")          '3
        Gridcomplaints.Columns.Add("status", "Status")                  '4
        Gridcomplaints.Columns.Add("ndname", "Case Name")               '5
        Gridcomplaints.Columns.Add("College_Name", "College Name")      '6
        Gridcomplaints.Columns.Add("State", "State")                    '7

        Gridcomplaints.Columns.Add("sortingNumber", "Sort Number")      '8
        Gridcomplaints.Columns.Add("followup", "Follow Up")            '9
        Gridcomplaints.Columns.Add("DATESORT", "DATESORT")              '10
        Gridcomplaints.Columns.Add("combinedsort", "combinedsort")      '11

        Gridcomplaints.Columns("dateandtime").Width = 125
        Gridcomplaints.Columns("Supervisor").Width = 0
        Gridcomplaints.Columns("comptype").Width = 125
        Gridcomplaints.Columns("status").Width = 60
        Gridcomplaints.Columns("comp_no").Width = 85
        Gridcomplaints.Columns("ndname").Width = 150
        Gridcomplaints.Columns("College_Name").Width = 250
        Gridcomplaints.Columns("State").Width = 150

        Gridcomplaints.Columns("followup").Width = 0
        Gridcomplaints.Columns("DATESORT").Width = 0
        Gridcomplaints.Columns("sortingNumber").Width = 0
        Gridcomplaints.Columns("combinedsort").Width = 0
        Gridcomplaints.Columns("followup").Visible = False
        Gridcomplaints.Columns("DATESORT").Visible = False
        Gridcomplaints.Columns("sortingNumber").Visible = False
        Gridcomplaints.Columns("combinedsort").Visible = False
        Gridcomplaints.Columns("Supervisor").Visible = False


        Gridcomplaints.Columns("dateandtime").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("comptype").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("status").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("comp_no").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("ndname").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("College_Name").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("State").SortMode = DataGridViewColumnSortMode.NotSortable

        Gridcomplaints.Columns("DATESORT").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("sortingNumber").SortMode = DataGridViewColumnSortMode.NotSortable
        Gridcomplaints.Columns("combinedsort").SortMode = DataGridViewColumnSortMode.NotSortable

    End Sub
    Function getlastcomm(ByVal compno As String) As String
        Dim lastcomm As String = ""
        Dim dtcom As DataTable = dataobject.getdata("select dateandtime from complaint_loginfo where complaintno='" & compno & "' order by dateandtime desc")
        If dtcom.Rows.Count > 0 Then
            lastcomm = Format(dtcom.Rows(0).Item("dateandtime"), "dd-MMM-yyyy")
        End If
        getlastcomm = lastcomm
    End Function
    Function getlaststatus(ByVal compno As String, ByVal lastcomm As String)
        getlaststatus = ""
        lastcomm = Format(lastcomm, "yyyy-MM-dd hh:mm tt")

        Dim dts As DataTable = dataobject.getdata("select status from complaint_loginfo where complaintno='" & compno & "'  order by dateandtime desc")
        Dim i As Integer = 0
        If dts.Rows.Count > 0 Then
            getlaststatus = Trim(dts.Rows(i).Item("status") & "")
        End If
    End Function
    Public Sub fill()
        Try
            lock()
            Gridcomplaints.ClearSelection()
            Dim newlogstatus As String
            Dim k As Integer
            Dim i As Integer
            gridformat()
            Gridcomplaints.DefaultCellStyle.BackColor = Color.LightGoldenrodYellow
            Dim dt2 As DataTable = dataobject.getdata("SELECT * FROM student_complaint_master  WHERE Call_date is not null and Call_date <> '' and duplicatecomp is null and comp_no NOT IN (SELECT Complaint_no FROM Complaint_Closure_Requests) ORDER BY Call_date desc ")
            If dt2.Rows.Count > 0 Then
                While dt2.Rows.Count > k
                    newlogstatus = bl.getlaststatusGRID(dt2.Rows(k).Item("comp_no"))
                    If Trim(UCase(newlogstatus)) <> "CLOSED" Then
                        Dialog2.prgbar.Minimum = 0
                        Dialog2.prgbar.Maximum = 100
                        Dialog2.prgbar.Value = k / dt2.Rows.Count * 100
                        Gridcomplaints.Rows.Add()
                        Gridcomplaints.Rows(i).Cells("dateandtime").Value = getlastcomm(Convert.ToString(dt2.Rows(k).Item("comp_no")))
                        Gridcomplaints.Rows(i).Cells("comptype").Value = Trim(IIf(IsDBNull(dt2.Rows(k).Item("comptype")), "NORMAL", dt2.Rows(k).Item("comptype"))) & ""
                        Gridcomplaints.Rows(i).Cells("status").Value = getlaststatus(Convert.ToString(dt2.Rows(k).Item("comp_no")), Gridcomplaints.Rows(i).Cells("dateandtime").Value)
                        Gridcomplaints.Rows(i).Cells("comp_no").Value = Convert.ToString(dt2.Rows(k).Item("comp_no"))
                        Gridcomplaints.Rows(i).Cells("ndname").Value = Trim(dt2.Rows(k).Item("ndname") & "").ToString()
                        Gridcomplaints.Rows(i).Cells("College_Name").Value = dt2.Rows(k).Item("College_Name").ToString()
                        Gridcomplaints.Rows(i).Cells("State").Value = Convert.ToString(dt2.Rows(k).Item("State"))
                        Gridcomplaints.Rows(i).Cells("DATESORT").Value = DateTime.Parse(Gridcomplaints.Rows(i).Cells("dateandtime").Value).ToString("yyyyMMddhhmmss")
                        Gridcomplaints.Rows(i).Cells("combinedsort").Value = newlogstatus
                        i = i + 1
                    End If
                    k += 1
                    Application.DoEvents()
                    f = 1
                End While
            End If
            ColorForNew()
            unlock()
            Exit Sub
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information)
        End Try
    End Sub
    Private Sub btnrefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrefresh.Click
        fill()
    End Sub

    Private Sub Managecases_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        If lblactivate.Text <> "" Then
            lblactivate.Text = ""
            fill()
        End If
    End Sub
    Private Sub Managecases_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cbosearch.Items.Clear()
        cbosearch.Items.Add("Select Criteria")
        cbosearch.Items.Add("Complaint No")
        cbosearch.Items.Add("Complainant Name")
        cbosearch.Items.Add("Complaint Type")
        cbosearch.Items.Add("College Name")
        cbosearch.SelectedIndex = 0
        lblactivate.Text = "activateflag"
    End Sub
    Private Sub lock()
        Cursor.Hide()
        Dialog2.Show()

        Me.Enabled = False
    End Sub
    Private Sub unlock()
        Me.Enabled = True
        Cursor.Show()
        Dialog2.Close()
    End Sub
    Public Sub ColorForNew()
        For Me.i = 0 To Gridcomplaints.Rows.Count - 1
            If Gridcomplaints.Rows(i).Cells(11).Value <> "CC" And Gridcomplaints.Rows(i).Cells(11).Value <> "MA" Then
                For J = 0 To Gridcomplaints.ColumnCount - 1
                    Gridcomplaints.Columns.Equals(J)
                    Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.GreenYellow
                    If Gridcomplaints.Rows(i).Cells(4).Value Like "IN*" Then
                        Gridcomplaints.Rows(i).DefaultCellStyle.Font = New Font(Font, FontStyle.Bold)
                    End If
                Next
                Gridcomplaints.Rows(i).Cells(8).Value = 0 & Gridcomplaints.Rows(i).Cells(10).Value
            ElseIf Gridcomplaints.Rows(i).Cells(11).Value = "CC" Then
                For J = 0 To Gridcomplaints.ColumnCount - 1
                    Gridcomplaints.Columns.Equals(J)
                    Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.Orange
                    If Gridcomplaints.Rows(i).Cells(4).Value Like "IN*" Then
                        Gridcomplaints.Rows(i).DefaultCellStyle.Font = New Font(Font, FontStyle.Bold)
                    End If
                Next
                Gridcomplaints.Rows(i).Cells(8).Value = 1 & Gridcomplaints.Rows(i).Cells(10).Value

            ElseIf Gridcomplaints.Rows(i).Cells(11).Value = "MA" Then
                For J = 0 To Gridcomplaints.ColumnCount - 1
                    Gridcomplaints.Columns.Equals(J)
                    Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.White
                    If Gridcomplaints.Rows(i).Cells(4).Value Like "IN*" Then
                        Gridcomplaints.Rows(i).DefaultCellStyle.Font = New Font(Font, FontStyle.Bold)
                    End If
                Next
                Gridcomplaints.Rows(i).Cells(8).Value = 2 & Gridcomplaints.Rows(i).Cells(10).Value
            Else
                MsgBox("ERROR: No status found, Contact Software developers.")
            End If
        Next
        For Me.i = 0 To Gridcomplaints.Rows.Count - 1
            If Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.White Then
                Dim dt As DataTable = dataobject.getdata("select * From tblfollowupdate where complaintno = '" & Gridcomplaints.Rows(i).Cells("comp_no").Value & "' ")
                If dt.Rows.Count > 0 Then
                    followdatedifference = DateDiff("d", Now.Date(), dt.Rows(0).Item("followupdt"))
                    If Val(followdatedifference) < 0 Then
                        Gridcomplaints.Rows(i).Cells(8).Value = 4 & Gridcomplaints.Rows(i).Cells(10).Value ' "C" & i
                        For J = 0 To Gridcomplaints.ColumnCount - 1
                            Gridcomplaints.Columns.Equals(J)
                            Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.Yellow  '&H80C0FF
                        Next
                    ElseIf Val(followdatedifference) = 0 Then
                        Gridcomplaints.Rows(i).Cells(8).Value = 3 & Gridcomplaints.Rows(i).Cells(10).Value ' "D" & i
                        For J = 0 To Gridcomplaints.ColumnCount - 1
                            Gridcomplaints.Columns.Equals(J)
                            Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.Yellow
                        Next
                    End If
                End If
                Dim dt1 As DataTable = dataobject.getdata("select * from  complaint_loginfo  where complaintno='" & Gridcomplaints.Rows(i).Cells(4).Value & "' and transferflag = 'SMA'")
                If dt1.Rows.Count > 0 Then
                    Gridcomplaints.Rows(i).Cells(8).Value = 4 & Gridcomplaints.Rows(i).Cells(10).Value ' "C" & i
                    For J = 0 To Gridcomplaints.ColumnCount - 1
                        Gridcomplaints.Columns.Equals(J)
                        Gridcomplaints.Rows(i).DefaultCellStyle.BackColor = Color.Yellow  '&H80C0FF
                    Next
                End If
            End If
        Next
        Gridcomplaints.Sort(Gridcomplaints.Columns(8), System.ComponentModel.ListSortDirection.Descending)
    End Sub

    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
        Try
            Dim searchdata As Integer
            Dim i As Integer
            If txtentryno.Text = "" Then
                MessageBox.Show("Cannot Find With Empty TextBox.")
                Exit Sub
            End If
            Select Case cbosearch.Text
                Case "Complaint No"
                    searchdata = 4
                Case "Complainant Name"
                    searchdata = 5
                Case "Complaint Seriousness"
                    searchdata = 2
                Case "College Name"
                    searchdata = 7
            End Select

            Dim TXTSRCH As String = ""
            Dim sstring As String
            For i = Val(lblstart.Text) To Gridcomplaints.Rows.Count - 1
                sstring = UCase(Gridcomplaints.Rows(i).Cells(searchdata).Value)
                If InStr(sstring, UCase(txtentryno.Text)) Then
                    Gridcomplaints.FirstDisplayedScrollingRowIndex = i
                    Gridcomplaints.Rows(i).Selected = True
                    TXTSRCH = "AK"
                    Exit For
                End If
            Next
            If TXTSRCH = "" Then
                lblstart.Text = 1
                MsgBox("The Specified " & Trim(cbosearch.Text) & " Not Found.Try Another.", vbInformation, "Search Result")
                lblstart.Text = "0"
                Exit Sub
            End If
            Gridcomplaints.FirstDisplayedScrollingRowIndex = i
            Gridcomplaints.Rows.Equals(i)
            Gridcomplaints.Columns.Equals(searchdata)

            Gridcomplaints.Focus()
            lblstart.Text = Val(i) + 1
        Catch ex As Exception
            MsgBox(ex.Message, vbInformation)
        End Try
    End Sub
    Private Sub Gridcomplaints_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Gridcomplaints.CellDoubleClick
        If e.RowIndex > -1 Then
            If Gridcomplaints.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.GreenYellow Then
                FollowupScreen.pnlRegulatoryAthority.BringToFront()
                FollowupScreen.lblCaseWork.Text = "Action Taken By Regulatory Authority"

            ElseIf Gridcomplaints.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Orange Then
                FollowupScreen.PnlCallCenter.BringToFront()
                FollowupScreen.lblCaseWork.Text = "Complaint Active At Call Center"
            Else
                FollowupScreen.pnlfollowUpAction.BringToFront()
                FollowupScreen.lblCaseWork.Text = "Follow-up Actions"
            End If
            FollowupScreen.lblcompNo.Text = Gridcomplaints.Rows(e.RowIndex).Cells("comp_no").Value
            FollowupScreen.ShowDialog()
        End If
    End Sub

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

    Private Sub cbosearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbosearch.Click
        cbosearch.Items.Remove("Select Criteria")
        cbosearch.SelectedIndex = 0
    End Sub
    Private Sub Gridcomplaints_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Gridcomplaints.CellClick
        lblstart.Text = "0"
    End Sub

End Class