private void btnAddStudent_Click(object sender, EventArgs e) { frmAdd_Edit frmadd = new frmAdd_Edit(); DialogResult rs = frmadd.ShowDialog(); if (frmadd.student.MSSV == null) { return; } // Mỗi lần Add là mỗi lần load cả List , có nên không nhỉ ??? ListViewItem St = new ListViewItem(STT.ToString()); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.MSSV.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.name.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.gender.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.birthday.ToShortDateString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.address.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.numberPhone.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.faculty.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.major.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.Class.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, frmadd.student.course.ToString())); this.lstStudent.Items.Add(St); StudentController.AddStudent(frmadd.student); STT++; }
private void btnEditStudent_Click(object sender, EventArgs e) { // Dùng để kiểm tra edit có bị trung MSSV không if (this.lstStudent.SelectedItems.Count <= 0) //??? { MessageBox.Show("Bạn chưa chọn đối tượng?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (this.lstStudent.Items.Count == 0) { return; } // Tìm ST với MSSV tương ứng trong database Student ST = new Student(); ST = StudentController.GetStudentbyID(this.lstStudent.SelectedItems[0].SubItems[1].Text); if (ST.MSSV == null) { return; } string oldMSSV = this.lstStudent.SelectedItems[0].SubItems[1].Text; frmAdd_Edit frmedit = new frmAdd_Edit(ref ST); DialogResult rs = frmedit.ShowDialog(); if (frmedit.edit == true) { // Tìm STT cho ST for (int i = 0; i < this.lstStudent.Items.Count; i++) { if (lstStudent.Items[i].SubItems[1].Text == ST.MSSV) { STT = i + 1; break; } } ListViewItem St = new ListViewItem(STT.ToString()); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.MSSV.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.name.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.gender.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.birthday.ToShortDateString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.address.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.numberPhone.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.faculty.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.major.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.Class.ToString())); St.SubItems.Add(new ListViewItem.ListViewSubItem(St, ST.course.ToString())); //Update StudentController.UpdateStudent(ST, oldMSSV); this.lstStudent.Items[this.lstStudent.SelectedItems[0].Index] = St; } }