private void simpleButtonSaveQuestion_Click(object sender, EventArgs e) { //if (BackgroundWorker != null) // BackgroundWorker.StartWork("Saving Question"); Cursor currentCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; int rowcount = 0; DataTable dt = gridControlQuestions.DataSource as DataTable; rowcount = dt.Rows.Count; if (!IsSaved) { m_oQuestion = new question(); m_oQuestion.created_by_user_id = UserSession.CurrentUser.UserId; } else { if (m_oQuestion == null) { m_oQuestion = BPContext.questions.Include("questions_text_language").FirstOrDefault(p => p.id == QuestionID); } } m_oQuestion.modified_by = UserSession.CurrentUser.UserId; m_oQuestion.modified_date = DateTime.Now; if (comboBoxEditQuestionGeneralValue.EditValue != null && !string.IsNullOrEmpty(comboBoxEditQuestionGeneralValue.EditValue.ToString())) m_oQuestion.general_value = Convert.ToByte(comboBoxEditQuestionGeneralValue.EditValue); DataRowView rowView = null; questions_text_language qtextlang; int question_id, question_text_language_id; bool exists = false; if (gridViewQuestion.RowCount > 0) { for (int x = 0; x < rowcount; ++x) { rowView = (DataRowView)gridViewQuestion.GetRow(x); if (rowView == null) continue; if (int.TryParse(rowView.Row["question_id"].ToString(), out question_id) && int.TryParse(rowView.Row["question_text_language_id"].ToString(), out question_text_language_id)) { qtextlang = m_oQuestion.questions_text_language.FirstOrDefault(p => p.questions_id == question_id && p.id == question_text_language_id); exists = true; } else { qtextlang = new questions_text_language(); exists = false; } qtextlang.question_text = rowView.Row["question"].ToString(); qtextlang.question_description = rowView.Row["description"].ToString(); qtextlang.question_help_text = rowView.Row["helptext"].ToString(); qtextlang.language_id = Convert.ToInt32(rowView.Row["language_id"]); if (!exists) { m_oQuestion.questions_text_language.Add(qtextlang); } } } else { BPContext.FIDeleteQuestion(string.Join(",", dataRowIds.ToArray())); if (this.ParentForm != null) { this.ParentForm.DialogResult = DialogResult.OK; } return; } //clean up non existent questions in the collection //m_oQuestion.questions_text_language.ToList().ForEach(delegate(questions_text_language qtl) { // if (dataRowIds.Contains(qtl.id)) { // BPContext.questions_text_language.DeleteObject(qtl); // } //}); BPContext.FIDeleteQuestion(string.Join(",", dataRowIds.ToArray())); dataRowIds.Clear(); m_oQuestion.questions_questiontags.ToList().ForEach(delegate(questions_questiontags _qqt) { BPContext.questions_questiontags.DeleteObject(_qqt); }); //var listqqts = m_oQuestion.questions_questiontags.ToList().Clone(); //List<int> listQQTIds = new List<int>(); questions_questiontags mQQT = null; if (dictionarySelectedTags != null) { foreach (KeyValuePair<string, int> kvp in dictionarySelectedTags) { //mQQT = listqqts.FirstOrDefault(p => p.questiontags_id == kvp.Value); //if (mQQT == null) { m_oQuestion.questions_questiontags.Add(new questions_questiontags() { questiontags_id = kvp.Value }); //} //listQQTIds.Add(kvp.Value); } //m_oQuestion.questions_questiontags.ToList().ForEach(delegate(questions_questiontags _qqt) { // if (!listQQTIds.Contains(_qqt.questiontags_id)) // BPContext.questions_questiontags.DeleteObject(_qqt); //}); } if (!IsSaved) { BPContext.questions.AddObject(m_oQuestion); IsSaved = true; } BPContext.SaveChanges(); //ParentControl.QuestionId = m_oQuestion.id; dt.Rows.Clear(); DataRow dr = null; m_oQuestion.questions_text_language.ForEach(delegate(questions_text_language dqtl) { dr = dt.NewRow(); dr["question_id"] = dqtl.questions_id; dr["question_text_language_id"] = dqtl.id; dr["language_id"] = dqtl.language_id; dr["question"] = dqtl.question_text; dr["description"] = dqtl.question_description; dr["helptext"] = dqtl.question_help_text; dt.Rows.Add(dr); }); dt.AcceptChanges(); if (QuestionID <= 0) QuestionID = m_oQuestion.id; groupControlAnswerForm.Enabled = true; simpleButtonSaveQuestion.Enabled = true; if (gridViewAnswerForm.RowCount > 0) { if(eSaveMode != SaveMode.Edit) btnEditAnswerform.Enabled = true; btnDeleteAnswerform.Enabled = true; } else { btnEditAnswerform.Enabled = false; btnDeleteAnswerform.Enabled = false; } Cursor.Current = currentCursor; //if (sender != null && sender.ToString() != "skip") // MessageBox.Show("Questions has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); //if (BackgroundWorker != null) BackgroundWorker.StopWork(); }
private void simpleButtonSaveQuestion_Click(object sender, EventArgs e) { //if (BackgroundWorker != null) // BackgroundWorker.StartWork("Saving Question"); Cursor currentCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; int rowcount = 0; DataTable dt = gridControlQuestions.DataSource as DataTable; rowcount = dt.Rows.Count; if (!IsSaved) { m_oQuestion = new question(); } else { if (m_oQuestion == null) { m_oQuestion = BPContext.questions.Include("questions_text_language").FirstOrDefault(p => p.id == QuestionID); } } m_oQuestion.modified_date = DateTime.Now; if (!string.IsNullOrEmpty(comboBoxEditQuestionGeneralValue.SelectedText)) m_oQuestion.general_value = Convert.ToByte(comboBoxEditQuestionGeneralValue.SelectedText); if (IsSaved) { //m_oQuestion exisiting questions m_oQuestion.questions_text_language.ToList().ForEach(qt => BPContext.questions_text_language.DeleteObject(qt)); } DataRowView rowView = null; questions_text_language qtextlang; for (int x = 0; x < rowcount; ++x) { rowView = (DataRowView)gridViewQuestion.GetRow(x); qtextlang = new questions_text_language(); qtextlang.language_id = int.Parse(rowView.Row["language"].ToString()); qtextlang.question_text = rowView.Row["question"].ToString(); qtextlang.question_description = rowView.Row["description"].ToString(); qtextlang.question_help_text = rowView.Row["helptext"].ToString(); m_oQuestion.questions_text_language.Add(qtextlang); } if (IsSaved) { //delete existing tags in this question m_oQuestion.questions_questiontags.ToList().ForEach(qt => BPContext.questions_questiontags.DeleteObject(qt)); } if (dictionarySelectedTags != null) { foreach (KeyValuePair<string, int> kvp in dictionarySelectedTags) { m_oQuestion.questions_questiontags.Add( new questions_questiontags() { questiontags_id = kvp.Value } ); } } if (!IsSaved) { BPContext.questions.AddObject(m_oQuestion); IsSaved = true; } BPContext.SaveChanges(); if (QuestionID <= 0) QuestionID = m_oQuestion.id; groupControlAnswerForm.Enabled = true; simpleButtonSaveQuestion.Enabled = true; Cursor.Current = currentCursor; MessageBox.Show("Questions has been saved.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); //if (BackgroundWorker != null) BackgroundWorker.StopWork(); }