protected void LoadGV(object p1, EventArgs p2)
        {
            var res = new MarkTable(db).GetTermTabulation(ddlTerm.SelectedValue);

            DataTable pivotTable = new DataTable();

            pivotTable.Columns.Add("Position");
            pivotTable.Columns.Add("Roll");
            //roll.AutoIncrement = true;
            //roll.AutoIncrementStep = 1;
            //roll.AutoIncrementSeed = 1;
            pivotTable.Columns.Add("First Name");
            pivotTable.Columns.Add("Last Name");

            Dictionary <string, List <TextValuePair> > subjectMarkPortions = new Dictionary <string, List <TextValuePair> >();

            var teacherSubjects = new TeacherSubjectTable(db).GetTeacherSubject(
                new YearClassSectionTable(db).GetYearClassSectionId(ddlYear.SelectedValue, ddlClass.SelectedValue, ddlSection.SelectedValue));

            foreach (var teacherSubject in teacherSubjects)
            {
                var portions = new MarkPortionTable(db).GetMarkPortion(teacherSubject.TeacherSubjectId);
                subjectMarkPortions.Add(teacherSubject.TeacherSubjectId, portions);
                headerLength.Add(teacherSubject.Name, portions.Count + 1);
                foreach (var portion in portions)
                {
                    pivotTable.Columns.Add(portion.Text + "|" + teacherSubject.TeacherSubjectId);
                }
                pivotTable.Columns.Add("Total|" + teacherSubject.TeacherSubjectId);
            }
            pivotTable.Columns.Add("Grand Total");

            var students     = new StudentTable(db).GetStudents(ddlYear.SelectedValue, ddlClass.SelectedValue, ddlSection.SelectedValue);
            var studentMarks = res.GroupBy(x => x.Student.ID).ToList();

            foreach (var student in students)
            {
                var studentMark = studentMarks.Find(x => x.Key == student.ID);
                var newRow      = pivotTable.Rows.Add();
                newRow["Roll"]       = student.Roll;
                newRow["First Name"] = student.FirstName;
                newRow["Last Name"]  = student.LastName;
                int  grandTotal = 0;
                bool termAbsent = true;
                if (studentMark == null)
                {
                    for (int i = 0; i < newRow.ItemArray.Length; i++)
                    {
                        newRow.ItemArray[i] = "A";
                    }
                }
                else
                {
                    foreach (var subjectMarkPortion in subjectMarkPortions)
                    {
                        int  subjectTotal  = 0;
                        bool subjectAbsent = true;
                        foreach (var portion in subjectMarkPortion.Value)
                        {
                            try {
                                int mark;
                                if (int.TryParse(studentMark.Where(x => x.PortionId == portion.Value).First().Mark, out mark) && mark >= 0)
                                {
                                    newRow[portion.Text + "|" + subjectMarkPortion.Key] = mark;
                                    subjectTotal += mark;
                                    subjectAbsent = false;
                                }
                                else
                                {
                                    newRow[portion.Text + "|" + subjectMarkPortion.Key] = "A";
                                }
                            } catch (Exception ex) {
                                newRow[portion.Text + "|" + subjectMarkPortion.Key] = "A";
                                Global.LogError(ex);
                            }
                        }
                        if (subjectAbsent)
                        {
                            newRow["Total|" + subjectMarkPortion.Key] = "A";
                        }
                        else
                        {
                            newRow["Total|" + subjectMarkPortion.Key] = subjectTotal;
                            termAbsent = false;
                        }
                        grandTotal += subjectTotal;
                    }
                }
                if (termAbsent)
                {
                    newRow["Grand Total"] = "A";
                }
                else
                {
                    newRow["Grand Total"] = grandTotal;
                }
            }

            pivotTable.DefaultView.Sort = "Grand Total DESC";

            gv.DataSource = pivotTable;
            gv.DataBind();
        }
        protected void LoadGridView(object sender, EventArgs e)
        {
            //var studentId = new UserTable<ApplicationUser>(db).GetUserId(User.Identity.Name);

            //var dataSource = (ddlSubject.SelectedValue == "all") ?
            //	db.Query("getMarkBySUIdYCSIdTId",
            //	new Dictionary<string, object>() {
            //		{ "@SUId", studentId },
            //		{ "@YCSId", ddlYear.SelectedValue },
            //		{"@TId", ddlTerm.SelectedValue } },
            //	true) :
            //	db.Query("getMarkBySUIdTidTSId",
            //	new Dictionary<string, object>() {
            //		{ "@SUId", studentId },
            //		{"@TSId", ddlSubject.SelectedValue },
            //		{"@TId", ddlTerm.SelectedValue } },
            //	true);
            //List<string> markPortions = dataSource.Select(x => x["Portion Name"]).Distinct().ToList();
            //DataTable pivotTable = new DataTable();
            //pivotTable.Columns.Add("Subject", typeof(string));
            //foreach (var item in markPortions) {
            //	pivotTable.Columns.Add(item, typeof(string));
            //}
            //var subjects = dataSource.GroupBy(x => x["Subject"]).ToList();
            //foreach (var item in subjects) {
            //	DataRow newRow = pivotTable.Rows.Add();
            //	newRow["Subject"] = item.Key;
            //	foreach (var item2 in item) {
            //		newRow[item2["Portion Name"]] = item2["Mark"];
            //	}
            //}
            var studentId = new StudentTable(db).GetStudentId(User.Identity.GetUserId());
            var SYCSRId   = new StudentYearClassSectionRollTable(db).
                            GetStudentYearClassSectionRollId(ddlYear.SelectedValue, studentId);
            var dataSource = new MarkTable(db).GetStudentMark(SYCSRId, ddlTerm.SelectedValue);

            List <string> markPortions = dataSource.Select(x => x.MarkPortionName).Distinct().ToList();
            DataTable     pivotTable   = new DataTable();

            pivotTable.Columns.Add("Subject", typeof(string));

            foreach (var markPortion in markPortions)
            {
                pivotTable.Columns.Add(markPortion, typeof(string));
            }
            pivotTable.Columns.Add("Total", typeof(string));
            pivotTable.Columns.Add("Grade", typeof(string));

            var subjects = dataSource.GroupBy(x => x.Subject.ToString(false)).ToList();

            foreach (var subject in subjects)
            {
                DataRow newRow = pivotTable.Rows.Add();
                newRow["Subject"] = subject.Key;
                int total = 0;
                foreach (var portion in subject)
                {
                    newRow[portion.MarkPortionName] = portion.Mark;
                    total += (int)(Convert.ToDouble(portion.Mark));
                }
                newRow["Total"] = total;
                string grade = null;
                if (total >= 80)
                {
                    grade = "A+";
                }
                else if (total >= 70)
                {
                    grade = "A";
                }
                else if (total >= 60)
                {
                    grade = "A-";
                }
                else if (total >= 50)
                {
                    grade = "B";
                }
                else if (total >= 40)
                {
                    grade = "C";
                }
                else if (total >= 33)
                {
                    grade = "D";
                }
                else
                {
                    grade = "F";
                }

                newRow["Grade"] = grade;
            }

            gvMark.DataSource = pivotTable;
            gvMark.DataBind();
        }