• 1405/05/21

تمرین جلسه_112 :

سلام و وقت بخیر 
فرم ادیت با دکمه حدف یکم متفاوت شد : 

using DataLayer.Context;
using DataLayer.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using TopLearn_School.People;

namespace TopLearn_School.Courses
{
    public partial class frmAddOrEditClass : Form
    {
        Toplearn_DB_Context _context = new Toplearn_DB_Context();
        public int classId = 0;
        bool _isEditMode = false;
        public frmAddOrEditClass()
        {
            InitializeComponent();
        }
        // دکمه انصراف
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        // زمان لود فرم 
        private void frmAddOrEditClass_Load(object sender, EventArgs e)
        {
            BindList();
            BindGrid();
            if (classId != 0)
            {
                _isEditMode = true;
                this.Text = "ویرایش کلاس";
                btnSave.Text = "ویرایش";

                var course = _context.Courses
                    .AsNoTracking()
                    .Single(p => p.CourseId == classId);
                txtTitle.Text = course.Title;
                txtDescription.Text = course.Description;
                txtStudentCount.Text = course.StudentCount.ToString();
                txtClassHours.Value = course.Hours;
                cboTeacher.SelectedValue = course.TeacherId;

                dgvStudentInClass.AutoGenerateColumns = false;
                BindStudentInClass();

            }
        }

        #region BindList
        // کمبو باکس معلمین
        void BindList()
        {
            cboTeacher.DataSource = _context.People
                .Where(a => a.IsTheacher == true)
                .Select(a => new { fullName = a.Name + " " + a.Family, a.PersonId }).ToList();
            cboTeacher.DisplayMember = "fullName";
            cboTeacher.ValueMember = "PersonId";
        }
        #endregion


        // دکمه نهایی فرم
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateInputs() == true)
            {
                Course course = new Course();
                course.Title = txtTitle.Text;
                course.Description = txtDescription.Text;
                course.Hours = int.Parse(txtClassHours.Value.ToString());
                course.StudentCount = int.Parse(txtStudentCount.Text);
                course.TeacherId = int.Parse(cboTeacher.SelectedValue.ToString());
                if (_isEditMode == false) // اینجا برای حالت افزودن
                {

                    _context.Courses.Add(course);
                    _context.SaveChanges();


                    if (dgvStudentInClass.Rows.Count > 0)
                    {
                        for (int i = 0; i < dgvStudentInClass.Rows.Count; i++)
                        {
                            string personId = dgvStudentInClass.Rows[i].Cells[0].Value.ToString();
                            StudentInCourse studentInCourse = new StudentInCourse()
                            {
                                CourseId = course.CourseId,
                                PersonId = int.Parse(personId),
                                RegisterDate = DateTime.Now
                            };
                            _context.StudentInCourses.Add(studentInCourse);

                        }

                    }
                }
                else // اینجا برای حال ویرایش که دکمه ویراش زده میشه
                {
                    course.CourseId = classId;
                    _context.Courses.Update(course);

                    if (dgvStudentInClass.Rows.Count > 0)
                    {
                        for (int i = 0; i < dgvStudentInClass.Rows.Count; i++)
                        {
                            string personId = dgvStudentInClass.Rows[i].Cells[0].Value.ToString();
                            StudentInCourse studentInCourse = new StudentInCourse()
                            {
                                CourseId = course.CourseId,
                                PersonId = int.Parse(personId),
                                RegisterDate = DateTime.Now
                            };
                            var studentInCalss = _context.StudentInCourses
                                .SingleOrDefault(a => a.CourseId == studentInCourse.CourseId
                            && a.PersonId == studentInCourse.PersonId);

                            if (studentInCalss == null)
                            {
                                _context.StudentInCourses.Add(studentInCourse);
                            }

                        }
                    }
                }
                _context.SaveChanges();
                Close();
            }
        }
        #region Validation 
        bool ValidateInputs()
        {
            bool IsValid = true;
            if (string.IsNullOrEmpty(txtTitle.Text))
            {
                txtTitle.BackColor = Color.Tomato;
                IsValid = false;
            }
            else
            {
                txtTitle.BackColor = Color.White;
            }
            if (string.IsNullOrEmpty(txtClassHours.Text)
                || txtClassHours.Value == 0)
            {
                txtClassHours.BackColor = Color.Tomato;
                IsValid = false;
            }
            else
            {
                txtClassHours.BackColor = Color.White;
            }
            if (string.IsNullOrEmpty(txtStudentCount.Text))
            {
                txtStudentCount.BackColor = Color.Tomato;
                IsValid = false;
            }
            else
            {
                txtStudentCount.BackColor = Color.White;
            }
            return IsValid;
        }
        #endregion

        #region BindStudentIn Calass
        // در اینجا گرید دانشجو های ثبت نامی که پر می شود
        void BindStudentInClass()
        {
            dgvStudentInClass.Rows.Clear();
            var student = _context.StudentInCourses
            .AsNoTracking().Where(p => p.CourseId == classId)
            .Select(s => new
            {
                s.PersonId,
                FullName = _context.People.AsNoTracking()
                     .Where(a => a.PersonId == s.PersonId)
                     .Select(w => w.Name + " " + w.Family).Single()

            }).ToList();
            foreach (var students in student)
            {
                dgvStudentInClass.Rows.Add(students.PersonId, students.FullName);
            }

        }
        #endregion


        //در اینجا لیست عضو های آکادمی تاپلرن در گرید نمایش می دهیم
        #region BindGrid
        void BindGrid()
        {
            dgvPersons.AutoGenerateColumns = false;
            dgvPersons.Rows.Clear();

            if (classId != 0) // اینجا برای تنظیم گرید عضوها در حالت ویراش که اینجا 3 بار به بانک کئوری زده شده
            {

                var CountStudent = _context.StudentInCourses.Where(a => a.CourseId == classId).Count();
                if (CountStudent > 0)
                {
                    var personId = _context.StudentInCourses
                        .AsNoTracking()
                        .Where(a => a.CourseId == classId)
                        .Select(z => z.PersonId).ToList();
                    var data = _context.People.ToList();
                    for (int i = 0; i < data.Count; i++)
                    {
                        bool result = true;
                        for (int j = 0; j < personId.Count; j++)
                        {
                            if (data[i].PersonId == personId[j])
                            {
                                result = false;
                                break;
                            }

                        }
                        if (result)
                        {
                            string fullName = data[i].Name + " " + data[i].Family;
                            dgvPersons.Rows
                          .Add(data[i].PersonId, fullName, data[i].Mobile, data[i].Email);
                        }
                    }


                }
                else // اینجا زمانی که دوره جدید می سازیم افزودن دوره
                {
                    var data = _context.People.ToList();
                    foreach (var item in data)
                    {
                        string fullName = item.Name + " " + item.Family;
                        dgvPersons.Rows.Add(item.PersonId, fullName, item.Mobile, item.Email);

                    }
                }
            }

        }
        #endregion
        // این تعداد دانشجو که باید حتما عدد باشد
        private void txtStudentCount_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int count = Convert.ToInt32(txtStudentCount.Text);
            }
            catch
            {
                txtStudentCount.Text = "";
            }
        }


        // اینجا دکمه افزودن دانشجو به دوره هستش
        private void btnAddStudent_Click(object sender, EventArgs e)
        {
            dgvStudentInClass.AutoGenerateColumns = false;
            if (dgvPersons.CurrentRow != null)
            {
                string personId = dgvPersons.CurrentRow.Cells[0].Value.ToString();
                string personName = dgvPersons.CurrentRow.Cells[1].Value.ToString();

                dgvStudentInClass.Rows.Add(personId, personName);
                dgvPersons.CurrentRow.Visible = false;
            }

        }
        //اینجا زمانی که دکمه حذف دانشجو از لیست ثبت نام شده زده میشه
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvStudentInClass.CurrentRow != null)
            {
                int personId = int.Parse(dgvStudentInClass.CurrentRow.Cells[0].Value.ToString());
                var removePerson = _context.StudentInCourses
                    .AsNoTracking()
                    .SingleOrDefault(a => a.PersonId == personId && a.CourseId == classId);
                if (removePerson != null)
                {
                    _context.StudentInCourses.Remove(removePerson);
                    _context.SaveChanges();
                }
                BindStudentInClass();
                BindGrid();

            }
        }
        //اینجا دکمه افزودن دانشجو در فرم ویرایش دوره قابلیت جدید
        private void btnAddperson_Click(object sender, EventArgs e)
        {
            frmAddPerson addPerson = new frmAddPerson();
            var reuslt = addPerson.ShowDialog();
            if (reuslt == DialogResult.OK)
            {
                BindGrid();
            }

        }
    }
}
// با تشکراز آموزش خوب  شما
  • 1405/05/21
  • ساعت 08:49

سلام

بسیار عالی