Public Class FRMREPORTS
    Private Sub cbteachersname_TextChanged(sender As Object, e As EventArgs) Handles cbteachersname.TextChanged
        Dim TeachersID = cbteachersname.SelectedValue
        Dim Query As String = "SELECT ANSWER, COUNT(`ANSWER`) as 'TOTAL' FROM tblanswer WHERE TEACHERID =  " & TeachersID.ToString() & " GROUP BY ANSWER"

        mySelectQuery(Query)
        LoadData(dgvresult, "result")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim TeachersID = cbteachersname.SelectedValue
        ' Dim Query As String = "SELECT ANSWER, COUNT(`ANSWER`) as 'TOTAL' FROM tblanswer WHERE TEACHERID =  " & TeachersID.ToString() & " GROUP BY ANSWER"

        ' Dim Row As DataGridViewRow = FindRowByMax(dgvresult, 1)
        ' Debug.WriteLine("Row 0 =====>" & Row.Cells(0).Value & " &&&&&&&&&& " & "Row 1 =====>" & Row.Cells(1).Value)

        ' Dim answer = FindRowByMax(dgvresult, 1).Cells(0).Value
        'Dim Query As String = "SELECT " & answer.ToString() & " AS 'ANSWER', MAX(ta.`TOTAL`) AS `TOTAL` FROM (SELECT TEACHERID, ANSWER, COUNT(`ANSWER`) as 'TOTAL' FROM tblanswer GROUP BY `ANSWER`) AS ta WHERE ta.TEACHERID =  " & TeachersID.ToString()
        'mySelectQuery("SELECT COUNT(`SECTION`) AS NUMBEROFSTUDENT, GRADE ,SECTION FROM tblschedule")


        'Dim Query As String = "SELECT Y.teacher, Y.GS, Y.mde, Y.ans FROM ( SELECT t.TEACHERSID as 'TID', t.TEACHERSNAME AS 'teacher', CONCAT( 'Grade ', s.GRADE, ' - ', s.SECTION ) AS 'GS', a.`ANSWER` AS mde, COUNT(a.`ANSWER`) AS ans FROM `tblanswer` a LEFT JOIN tblstudents s ON a.`STUDENTID` = s.IDNO LEFT JOIN tblteachers t ON a.`TEACHERID` = t.TEACHERSID GROUP BY s.SECTION, a.`ANSWER`, a.TEACHERID ORDER BY a.teacherid, ans DESC ) Y WHERE Y.TID = " & cbteachersname.SelectedValue & " GROUP BY Y.teacher, Y.GS "
        '"SELECT Y.teacher, Y.GS, Y.mde, Y.ans FROM ( SELECT t.TEACHERSID as 'TID', t.TEACHERSNAME AS 'teacher', CONCAT( 'Grade ', s.GRADE, ' - ', s.SECTION ) AS 'GS', a.`ANSWER` AS mde, COUNT(a.`ANSWER`) AS ans FROM `tblanswer` a LEFT JOIN tblstudents s ON a.`STUDENTID` = s.IDNO LEFT JOIN tblteachers t ON a.`TEACHERID` = t.TEACHERSID GROUP BY s.SECTION, a.`ANSWER`, a.TEACHERID ORDER BY a.teacherid, ans DESC ) Y WHERE Y.TID = " & cbteachersname.SelectedValue & " GROUP BY Y.teacher, Y.GS "

        ' mySelectQuery("SELECT y.mde, max(y.ans) FROM(SELECT  s.SECTION, a.`ANSWER` as mde, COUNT(a.`ANSWER`) as ans FROM  `tblanswer` a LEFT JOIN tblstudents s ON a.`STUDENTID` = s.IDNO WHERE  s.SECTION = 'earth' GROUP BY a.`ANSWER`  ORDER BY `ans` DESC) as y")

        'Debug.WriteLine("Query =====>" & Query)
        'myreporting("SELECT Y.teacher, Y.GS, Y.mde, Y.ans FROM ( SELECT t.TEACHERSID as 'TID', t.TEACHERSNAME AS 'teacher', CONCAT( 'Grade ', s.GRADE, ' - ', s.SECTION ) AS 'GS', a.`ANSWER` AS mde, COUNT(a.`ANSWER`) AS ans FROM `tblanswer` a LEFT JOIN tblstudents s ON a.`STUDENTID` = s.IDNO LEFT JOIN tblteachers t ON a.`TEACHERID` = t.TEACHERSID GROUP BY s.SECTION, a.`ANSWER`, a.TEACHERID ORDER BY a.teacherid, ans DESC ) Y WHERE Y.TID = " & cbteachersname.SelectedValue & " GROUP BY Y.teacher, Y.GS ", "tbireports")

            ' PrintReport(Query, "/Reports/tbireports.rpt", frmprint.CrystalReportViewer1)
        '

        report_form.TextBox1.Text = TeachersID.ToString
        report_form.Show()

    End Sub

    Private Sub FRMREPORTS_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        mySelectQuery("SELECT `TEACHERSID`, `TEACHERSNAME` FROM `tblteachers`")
        fillcombo(cbteachersname, "TEACHERSNAME", "TEACHERSID")
    End Sub

    Private Function FindRowByMax(ByVal dgv As DataGridView, ByVal colNum As Integer)
        Dim max As Integer
        Dim row As DataGridViewRow = Nothing

        For x As Integer = 0 To dgv.Rows.Count - 1
            If max = 0 Then
                max = dgv.Rows(x).Cells(colNum).Value
                row = dgv.Rows(x)
            Else
                If max < dgv.Rows(x).Cells(colNum).Value Then
                    max = dgv.Rows(x).Cells(colNum).Value
                    row = dgv.Rows(x)
                End If
            End If
        Next

        Return row
    End Function
End Class