public FormStudentList(FacultyUserModel gotFaculty, SectionModel gotSection) { InitializeComponent(); faculty = gotFaculty; section = gotSection; labelWelcome.Text = faculty.FullName; labelStudents.Text = "Students of " + section.SectionName; //try //{ UserController controller = new UserController(); studentList = controller.GetBySection(section); ClassController ccontroller = new ClassController(); classList = ccontroller.GetBySectionId(section.Id); AttendanceController acontroller = new AttendanceController(); attendanceList = acontroller.GetAllBySection(section.Id); Console.WriteLine("Found student: " + studentList.Count); Console.WriteLine("Found classes: " + classList.Count); Console.WriteLine("Found attendances: " + attendanceList.Count); List <StudentView> mainList = new List <StudentView>(); int i = 0; foreach (StudentUserModel student in studentList) { Console.WriteLine("doing student no. " + i); StudentView view = new StudentView(); view.AcademicId = student.AcademicId; view.FullName = student.FullName; int c = 0; foreach (ClassModel Class in classList) { AttendanceModel att = attendanceList.Find(x => (x.ClassId == Class.Id) && (x.StudentId == student.Id)); Console.WriteLine("Found attendance id: " + att.Id + " for: " + student.Id + " on: " + Class.Id + " entry: " + att.Entry); view.attendances.Add(att.Entry); DataGridViewColumn col = new DataGridViewTextBoxColumn(); col.HeaderText = Class.ClassDate; int colIndex = dataGridViewStudentList.Columns.Add(col); //dataGridViewStudentList.Rows[i].Cells[c+2].Value = att.Entry.ToString(); c++; } mainList.Add(view); i++; } Console.WriteLine("Main list count: " + mainList.Count); MySortableBindingList <StudentView> mainListBinding = new MySortableBindingList <StudentView>(mainList); dataGridViewStudentList.AutoGenerateColumns = false; dataGridViewStudentList.DataSource = mainListBinding; dataGridViewSerial.AutoGenerateColumns = false; dataGridViewSerial.DataSource = mainListBinding; dataGridViewSerial.Columns[0].DefaultCellStyle.Padding = new Padding(3, 0, 0, 0); dataGridViewStudentList.DataBindingComplete += (o, e) => { foreach (DataGridViewRow row in dataGridViewSerial.Rows) { row.Cells["sln"].Value = (row.Index + 1).ToString(); } foreach (DataGridViewColumn column in dataGridViewStudentList.Columns) { column.SortMode = DataGridViewColumnSortMode.Programmatic; } }; foreach (DataGridViewColumn column in dataGridViewStudentList.Columns) { column.SortMode = DataGridViewColumnSortMode.Automatic; } //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message); //} }
public FormStudentList(FacultyUserModel gotFaculty, SectionModel gotSection) { InitializeComponent(); faculty = gotFaculty; section = gotSection; labelWelcome.Text = faculty.FullName; labelStudents.Text = "Students of " + section.SectionName; try { UserController controller = new UserController(); studentList = controller.GetBySection(section); ClassController ccontroller = new ClassController(); classList = ccontroller.GetBySectionId(section.Id); AttendanceController acontroller = new AttendanceController(); attendanceList = acontroller.GetAllBySection(section.Id); //Console.WriteLine("Found student: " + studentList.Count); //Console.WriteLine("Found classes: " + classList.Count); //Console.WriteLine("Found attendances: " + attendanceList.Count); DataGridViewColumn sln = new DataGridViewTextBoxColumn(); sln.HeaderText = "#"; sln.Width = 30; sln.ReadOnly = true; dataGridViewSln.Columns.Add(sln); DataGridViewColumn IdCol = new DataGridViewTextBoxColumn(); IdCol.HeaderText = "ID"; IdCol.Width = 75; IdCol.ReadOnly = true; dataGridViewStudentList.Columns.Add(IdCol); DataGridViewColumn FullNameCol = new DataGridViewTextBoxColumn(); FullNameCol.HeaderText = "Name"; FullNameCol.Width = 200; FullNameCol.ReadOnly = true; dataGridViewStudentList.Columns.Add(FullNameCol); foreach (ClassModel Class in classList) { DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn(); DateTime date = DateTime.Parse(Class.ClassDate); string dateMod = date.ToString("MMM-dd"); col.HeaderText = dateMod; col.ReadOnly = false; col.Width = 50; col.MaxInputLength = 1; col.ValueType = typeof(string); col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; dataGridViewStudentList.Columns.Add(col); } int i = 0; foreach (StudentUserModel student in studentList) { //Console.WriteLine("doing student no. " + i); StudentView view = new StudentView(); view.AcademicId = student.AcademicId; view.FullName = student.FullName; dataGridViewStudentList.Rows.Add(); dataGridViewSln.Rows.Add(); dataGridViewSln.Rows[i].Cells[0].Value = i + 1; dataGridViewStudentList.Rows[i].Cells[0].Value = student.AcademicId; dataGridViewStudentList.Rows[i].Cells[1].Value = student.FullName; int c = 0; foreach (ClassModel Class in classList) { AttendanceModel att = attendanceList.Find(x => (x.ClassId == Class.Id) && (x.StudentId == student.Id)); //Console.WriteLine("Found attendance id: " + att.Id + " for: " + student.Id + " on: " + Class.Id + " entry: " + att.Entry); if (att.Entry == 2) { dataGridViewStudentList.Rows[i].Cells[c + 2].Value = "L"; } else { dataGridViewStudentList.Rows[i].Cells[c + 2].Value = att.Entry.ToString(); } c++; } i++; } MySortableBindingList <StudentUserModel> studentListSortable = new MySortableBindingList <StudentUserModel>(studentList); dataGridViewStudentList.DataBindingComplete += (o, e) => { foreach (DataGridViewColumn column in dataGridViewStudentList.Columns) { column.SortMode = DataGridViewColumnSortMode.Programmatic; } }; foreach (DataGridViewColumn column in dataGridViewStudentList.Columns) { column.SortMode = DataGridViewColumnSortMode.Automatic; } } catch (Exception ex) { MessageBox.Show(ex.Message); } if (classList.Count != 0) { DataGridViewTextBoxColumn totalCol = new DataGridViewTextBoxColumn(); totalCol.HeaderText = "Total"; totalCol.Width = 38; totalCol.ReadOnly = true; totalCol.Name = "Total"; totalCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; dataGridViewStudentList.Columns.Add(totalCol); foreach (DataGridViewRow row in dataGridViewStudentList.Rows) { int sum = 0; int classNo = 0; foreach (ClassModel Class in classList) { if (row.Cells[classNo + 2].Value.ToString() == "L" || row.Cells[classNo + 2].Value.ToString() == "l") { sum += 1; } else if (row.Cells[classNo + 2].Value.ToString() == "1") { sum += 1; } classNo++; } row.Cells["Total"].Value = sum.ToString(); } } }