-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExamQuestionGridForm.cs
79 lines (78 loc) · 3.48 KB
/
ExamQuestionGridForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using ExamProj.Class;
using ExamProj.Context;
using ExamProj.Repositories;
using ExamProj.Services;
using ExamProj.Services.Interfaces;
using System;
using System.Linq;
using System.Windows.Forms;
namespace ExamProj
{
public partial class ExamQuestionGridForm : DevExpress.XtraEditors.XtraForm
{
public Question question = new Question();
private IQuestionServices _questionServices { get; set; }
public ExamQuestionGridForm()
{
InitializeComponent();
_questionServices = new QuestionServices(new Repository<Question>(new ExamDbContext()));
RefreshQuestions();
}
public void RefreshQuestions()
{
gridControl.DataSource = _questionServices.GetAllQuestions().ToList();
}
private void gridControl_DoubleClick(object sender, EventArgs e)
{
if (gridView.FocusedRowHandle > -1)
{
var selectedQuestion = Convert.ToInt32(gridView.GetFocusedRowCellValue("ID"));
question = _questionServices.GetQuestionByID(selectedQuestion);
ExamQuestionCRUDForm form = new ExamQuestionCRUDForm(question, _questionServices);
form.ShowDialog();
RefreshQuestions();
question = new Question();
}
else
MessageBox.Show("Select a question.", "Incorrect Line", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
private void newBtn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
ExamQuestionCRUDForm form = new ExamQuestionCRUDForm(question, _questionServices);
form.ShowDialog();
RefreshQuestions();
question = new Question();
}
private void deleteBtn_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
DialogResult result;
if (gridView.SelectedRowsCount > 0)
{
result = MessageBox.Show("Are you sure about deleting these questions?", "Delete Question?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
foreach (var row in gridView.GetSelectedRows())
{
var selectedQuestion = Convert.ToInt32(gridView.GetRowCellValue(row, "ID"));
_questionServices.DeleteQuestion(selectedQuestion);
}
MessageBox.Show($"Questions were deleted.", "Question Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
RefreshQuestions();
}
}
else if (gridView.FocusedRowHandle > -1)
{
result = MessageBox.Show("Are you sure about deleting this question?", "Delete Question?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
var selectedQuestion = Convert.ToInt32(gridView.GetFocusedRowCellValue("ID"));
_questionServices.DeleteQuestion(selectedQuestion);
RefreshQuestions();
MessageBox.Show($"Question {selectedQuestion} was deleted.", "Question Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
MessageBox.Show("Select a question.", "Incorrect Line", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}