private void btnAddStudentToGroup_Click(object sender, EventArgs e) { try { if (GroupsDataGridView.CurrentRow.IsNewRow) { throw new Exception("You cannot add students to non-existant group!"); } int rowIndex = GroupsDataGridView.SelectedCells[0].RowIndex; AddStudentToGroupDialog dialog = new AddStudentToGroupDialog(GroupsDataGridView["Grade", rowIndex].EditedFormattedValue.ToString()); //dialog.Owner = this; dialog.FormClosed += (o, s) => { if (dialog.StudentID != 0) { //studentsDataGridView.Rows.Add(dialog.GradeSection, dialog.LastName, dialog.FirstName, dialog.StudentID); //add new student try { GroupDAL.AddStudentToGroup( int.Parse(GroupsDataGridView["GroupID", rowIndex].Value.ToString()), dialog.StudentID); this.getGroupStudentsTableAdapter.Fill(this.schooljournalDataSet.getGroupStudents, int.Parse(GroupsDataGridView["GroupID", rowIndex].Value.ToString())); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { } } }; dialog.ShowDialog(); } catch (Exception ex) { string msg = ex.Message; if (ex.InnerException != null) { msg += "\n" + ex.InnerException.Message; } MessageBox.Show(msg, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }