Ejemplo n.º 1
0
        private void dgvBranch_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            int id = 0;

            if (int.TryParse(e.Row.Cells[0].Value.ToString(), out id))
            {
                var db = new LINQDataContext();
                // Define the query expression.
                IEnumerable <int> qGroupID =
                    from groups in db.Groups
                    where groups.Branch_Selection == id
                    select groups.ID;
                //
                // Define the query expression.
                IEnumerable <int> qCourseID =
                    from course in db.Courses
                    where course.Branch_ID == id
                    select course.Course_ID;
                //
                // if exist data in the Groups table :
                //
                if (qGroupID.ToArray().Length > 0)
                {
                    string groups_ID_List = "";
                    foreach (var anyGroup in qGroupID)
                    {
                        groups_ID_List += anyGroup.ToString() + " and ";
                    }
                    groups_ID_List = groups_ID_List.Substring(0, groups_ID_List.Length - 4);

                    e.Cancel = true;

                    MessageBox.Show("Information Groups with group numbers " +
                                    groups_ID_List + "is related to Branch (" +
                                    ((e.Row.Cells[1].Value != null) ? e.Row.Cells[1].Value.ToString() : "NULL") +
                                    ").\n First, this information can change groups.", "Delete Row Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                }
                //
                // if exist data in the Groups table :
                //
                else if (qCourseID.ToArray().Length > 0)
                {
                    string course_ID_List = "";
                    foreach (var anyCourse in qCourseID)
                    {
                        course_ID_List += anyCourse.ToString() + " and ";
                    }
                    course_ID_List = course_ID_List.Substring(0, course_ID_List.Length - 4);

                    e.Cancel = true;

                    MessageBox.Show("Information courses with course numbers " +
                                    course_ID_List + "is related to Branch (" +
                                    ((e.Row.Cells[1].Value != null) ? e.Row.Cells[1].Value.ToString() : "NULL") +
                                    ").\n First, this information can change courses.", "Delete Row Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    db.BranchDelete(id);
                }
                db.Dispose();
            }
        }
        private void dgvBranch_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            int id = 0;
            if (int.TryParse(e.Row.Cells[0].Value.ToString(), out id))
            {
                var db = new LINQDataContext();
                // Define the query expression.
                IEnumerable<int> qGroupID =
                    from groups in db.Groups
                    where groups.Branch_Selection == id
                    select groups.ID;
                //
                // Define the query expression.
                IEnumerable<int> qCourseID =
                    from course in db.Courses
                    where course.Branch_ID == id
                    select course.Course_ID;
                //
                // if exist data in the Groups table :
                //
                if (qGroupID.ToArray().Length > 0)
                {
                    string groups_ID_List = "";
                    foreach (var anyGroup in qGroupID)
                    {
                        groups_ID_List += anyGroup.ToString() + " and ";
                    }
                    groups_ID_List = groups_ID_List.Substring(0, groups_ID_List.Length - 4);

                    e.Cancel = true;

                    MessageBox.Show("Information Groups with group numbers " +
                        groups_ID_List + "is related to Branch (" +
                        ((e.Row.Cells[1].Value != null) ? e.Row.Cells[1].Value.ToString() : "NULL") +
                        ").\n First, this information can change groups.", "Delete Row Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                }
                //
                // if exist data in the Groups table :
                //
                else if (qCourseID.ToArray().Length > 0)
                {
                    string course_ID_List = "";
                    foreach (var anyCourse in qCourseID)
                    {
                        course_ID_List += anyCourse.ToString() + " and ";
                    }
                    course_ID_List = course_ID_List.Substring(0, course_ID_List.Length - 4);

                    e.Cancel = true;

                    MessageBox.Show("Information courses with course numbers " +
                        course_ID_List + "is related to Branch (" +
                        ((e.Row.Cells[1].Value != null) ? e.Row.Cells[1].Value.ToString() : "NULL") +
                        ").\n First, this information can change courses.", "Delete Row Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
                }
                else
                {
                    db.BranchDelete(id);
                }
                db.Dispose();
            }
        }