public void FillComboStudent() { CommonSP stud = new CommonSP(); DataTable dt = new DataTable(); dt = stud.FillStudents(classId); DataRow drow = dt.NewRow(); dt.Rows.InsertAt(drow, 0); dgvcmbstudent.DataSource = dt; dgvcmbstudent.ValueMember = "ledgerId"; dgvcmbstudent.DisplayMember = "ledgerName"; }
/// <summary> /// dgvAttendance grid cell begin edit for Combo box student fill and remove from the list once it has selected /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvAttendance_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { try { if (dgvAttendance.RowCount > 1) { DataTable dtbl = new DataTable(); CommonSP studentSp = new CommonSP(); if (dgvAttendance.CurrentCell.ColumnIndex == dgvAttendance.Columns["dgvcmbstudent"].Index) { dtbl = studentSp.FillStudents(classId); if (dtbl.Rows.Count > 0) { if (dgvAttendance.RowCount > 1) { int inGridRowCount = dgvAttendance.RowCount; for (int inI = 0; inI < inGridRowCount - 1; inI++) { if (inI != e.RowIndex) { int inTableRowcount = dtbl.Rows.Count; for (int inJ = 0; inJ < inTableRowcount; inJ++) { if (dgvAttendance.Rows[inI].Cells["dgvcmbstudent"].Value != null && dgvAttendance.Rows[inI].Cells["dgvcmbstudent"].Value.ToString() != string.Empty) { if (dtbl.Rows[inJ]["ledgerId"].ToString() == dgvAttendance.Rows[inI].Cells["dgvcmbstudent"].Value.ToString()) { dtbl.Rows.RemoveAt(inJ); break; } } } } } } DataGridViewComboBoxCell dgvccStudent = (DataGridViewComboBoxCell)dgvAttendance[dgvAttendance.Columns["dgvcmbstudent"].Index, e.RowIndex]; dgvccStudent.DataSource = dtbl; dgvccStudent.ValueMember = "ledgerId"; dgvccStudent.DisplayMember = "ledgerName"; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }