private void GenerateTasks()
 {
     using (Modelss.AKADEMIYAEntities2 db = new Modelss.AKADEMIYAEntities2())
     {
         dvgsmark2.Rows.Clear();
         List <Modelss.Student> students = db.Students.Where(s => s.GroupId == _groupID).ToList();
         foreach (var item in students)
         {
             List <Modelss.StudentsMark> studentsMark = db.StudentsMarks.Where(sm => sm.StudentId == item.Id && sm.TaskId == _taskID).ToList();
             Modelss.Task task     = item.Group.Tasks.FirstOrDefault(t => t.Id == _taskID);
             string       taskName = "";
             int          taskMark = 0;
             if (task != null)
             {
                 taskName = task.Name;
                 Modelss.StudentsMark smark = task.StudentsMarks.FirstOrDefault(sm => sm.StudentId == item.Id);
                 if (smark != null)
                 {
                     taskMark = smark.Mark;
                 }
             }
             dvgsmark2.Rows.Add(item.Id, task.Id, item.Name, item.Surname, taskName, taskMark);
         }
     }
 }
        private void Btnwrite_Click(object sender, EventArgs e)
        {
            if (btnwrite.Text == "Update")
            {
                if (!UpdateMark())
                {
                    MessageBox.Show("Not Submitted");
                    cbg.Text = txtMark.Text = "";

                    return;
                }
                MessageBox.Show("Submitted");
                FilldvgMarks();

                FilldvgAverage();
                btnwrite.Text = "Submit";
                cbg.Text      = txtMark.Text = "";
                return;
            }

            string sname    = dvgstudent2.CurrentRow.Cells[1].Value.ToString();
            string ssurname = dvgstudent2.CurrentRow.Cells[2].Value.ToString();
            int    id       = Convert.ToInt32(dvgstudent2.CurrentRow.Cells[0].Value);

            using (Modelss.AKADEMIYAEntities2 db = new Modelss.AKADEMIYAEntities2())
            {
                Modelss.StudentsMark sm = new Modelss.StudentsMark();
                int taskid = Convert.ToInt32(db.Tasks.Where(t => t.Name == cbg.Text).FirstOrDefault().Id);

                sm.Mark = Convert.ToInt32(txtMark.Text);

                sm.StudentId = id;
                sm.TaskId    = taskid;
                db.StudentsMarks.Add(sm);
                db.SaveChanges();
            }
            FilldvgMarks();

            FilldvgAverage();
            txtMark.Text = cbg.Text = " ";
        }
        private void Btnmark2_Click(object sender, EventArgs e)
        {
            int affectedRows = 0;

            using (Modelss.AKADEMIYAEntities2 db = new Modelss.AKADEMIYAEntities2())
            {
                smm.Id = (int)dvgsmark2.CurrentRow.Cells[0].Value;
                Modelss.StudentsMark SM = db.StudentsMarks.Where(s => s.StudentId == smm.Id).FirstOrDefault();

                //validation only numbers
                String patternnum = "^[0-9]+(,[0-9]+)*$";
                Regex  mark       = new Regex(patternnum);
                Match  numMatch   = mark.Match(txtmark.Text);
                if (!numMatch.Success)
                {
                    MessageBox.Show("Yalniz ededler daxil edin");
                    txtmark.Text = "";
                    return;
                }
                SM.Mark      = Convert.ToInt32(txtmark.Text);
                SM.StudentId = (int)dvgsmark2.CurrentRow.Cells[0].Value;
                SM.TaskId    = (int)dvgsmark2.CurrentRow.Cells[1].Value;
                affectedRows = db.SaveChanges();
                //dvgsmark2.CurrentRow.Cells[5].Value = txtmark.Text;
                btnmark2.Enabled = false;
                txtmark.Text     = "";
                GenerateTasks();
            }
            if (affectedRows > 0)
            {
                MessageBox.Show("success");
                return;
            }
            else
            {
                MessageBox.Show("error");
                return;
            }
        }
        private bool UpdateMark()
        {
            int affectedRows = 0;

            using (Modelss.AKADEMIYAEntities2 db = new Modelss.AKADEMIYAEntities2())
            {
                studentMark.StudentId = Convert.ToInt32(dvgMarks.CurrentRow.Cells[1].Value);
                Modelss.StudentsMark studenmarks = db.StudentsMarks.Where(m => m.StudentId == studentMark.StudentId).FirstOrDefault();
                int taskid = Convert.ToInt32(db.Tasks.Where(t => t.Name == cbg.Text).FirstOrDefault().Id);
                studenmarks.Mark      = Convert.ToInt32(txtMark.Text);
                studenmarks.TaskId    = taskid;
                studenmarks.StudentId = Convert.ToInt32(dvgMarks.CurrentRow.Cells[1].Value);

                affectedRows = db.SaveChanges();
            }
            if (affectedRows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }