private questions QuestionSave(questions question, int link_id, int count_view, int subject_id, int user_id) { if (question == null) { question = db.Add(new questions(link_id, count_view, subject_id, user_id)).Entity; } else { question.link_id = link_id; question.count_view = count_view; question.subject_id = subject_id; question.user_id = user_id; } db.SaveChanges(); return(question); }
private void data_grid_view_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { var row = data_grid_view.Rows[e.RowIndex]; if (data_grid_view.IsCurrentRowDirty) { if (row.Cells["status"].Value == null) { row.Cells["status"].Value = "Hide"; } if (row.Cells["count_view"].Value == null || row.Cells["count_view"].Value is DBNull) { row.Cells["count_view"].Value = 0; } if (row.Cells["login"].Value == null || row.Cells["subject"].Value == null || row.Cells["age"].Value == null || row.Cells["age"].Value is DBNull || row.Cells["link_id"].Value == null || row.Cells["link_id"].Value is DBNull || (int)row.Cells["age"].Value < 0 || (int)row.Cells["link_id"].Value < 0) { e.Cancel = true; if (MessageBox.Show("Введены некорректные данные \n\nВернуть предыдущие значения?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.Yes) { row.Cells["link_id"].Value = ((List <object>)row.Tag)[1]; row.Cells["subject"].Value = ((List <object>)row.Tag)[4]; row.Cells["login"].Value = ((List <object>)row.Tag)[6]; row.Cells["age"].Value = ((List <object>)row.Tag)[7]; } } if (!e.Cancel) { statuses status = db.statuses.Where(status => status.name == (string)row.Cells["status"].Value).First(); users user = db.users.Where(user => user.login == (string)row.Cells["login"].Value).FirstOrDefault(); questions question = db.questions.Where(question => question.id_question == (int?)row.Cells["id_question"].Value).FirstOrDefault(); subjects subject = db.subjects.Where(subject => subject.name == (string)row.Cells["subject"].Value).First(); user = UserSave(user, (string)row.Cells["login"].Value, (int)row.Cells["age"].Value, status.id_status); question = QuestionSave(question, (int)row.Cells["link_id"].Value, (int)row.Cells["count_view"].Value, subject.id, user.id_user); if (row.Cells["id_question"].Value != null) //if a tag of the mutable row is null, then this row is new { UpdateUserInfoDataGrid(row, status.id_status); } else { row.Cells["id_question"].Value = question.id_question; row.Cells["id_question"].Value = subject.id; row.Cells["user_id"].Value = user.id_user; row.Cells["status_id"].Value = user.statuses.id_status; } var list = new List <object>(); foreach (DataGridViewCell cell in row.Cells) { list.Add(cell.Value); } row.Tag = list; //change a tag this row row.ErrorText = string.Empty; //clear error text row.Cells["age"].ReadOnly = false; row.Cells["status"].ReadOnly = false; } } }