-
Notifications
You must be signed in to change notification settings - Fork 1
/
TypeMaster.cs
145 lines (129 loc) · 4.45 KB
/
TypeMaster.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Quiz
{
public partial class TypeMaster : Form
{
public TypeMaster()
{
InitializeComponent();
}
#region Form Events
private void TypeMaster_Load(object sender, EventArgs e)
{
PopulateGridView();
}
#endregion
#region Helper Methods
/// <summary>
/// This method Populated the Grid View with data from TypeMaster Table
/// </summary>
private void PopulateGridView()
{
dgv_typeMaster.Rows.Clear();
DataSet masterData = DataLayer.GetTypeMaster();
foreach (DataRow row in masterData.Tables[0].Rows)
{
dgv_typeMaster.Rows.Add();
dgv_typeMaster.Rows[dgv_typeMaster.Rows.Count - 1].Cells[0].Value = row["Id"];
dgv_typeMaster.Rows[dgv_typeMaster.Rows.Count - 1].Cells[1].Value = row["TypeName"];
PrepareRow(dgv_typeMaster.CurrentRow.Index);
}
dgv_typeMaster.Rows.Add();
PrepareRow(dgv_typeMaster.CurrentRow.Index);
}
/// <summary>
/// This method Prepopulates Button text in a Row
/// </summary>
/// <param>
/// <c>index</c> The index of the row to be processed
/// </param>
private void PrepareRow(int index)
{
dgv_typeMaster.Rows[index].Cells[2].Value = "Edit";
dgv_typeMaster.Rows[index].Cells[3].Value = "Delete";
}
/// <summary>
/// This method Diables or Enables Editing of a given Row
/// </summary>
/// <param>
/// <c>row</c> is the Index of Perform the Action on.
/// </param>
/// /// <param>
/// <c>Disable</c> contains value for Disble/Enable.
/// </param>
private void DisableEdit(int row, bool Disable)
{
if (!Disable && (row == dgv_typeMaster.Rows.Count - 1))
{
dgv_typeMaster.Rows.Add();
PrepareRow(dgv_typeMaster.Rows.Count - 1);
}
dgv_typeMaster.Rows[row].Cells[1].ReadOnly = Disable;
dgv_typeMaster.Rows[row].Cells[1].Style.BackColor = Disable ? Color.White : Color.Tan;
}
/// <summary>
/// This method Deletes a Row from the Data Grid View.
/// </summary>
/// <param>
/// <c>index</c> is index of the row to be deleted.
/// </param>
private void DeleteRow(int index)
{
dgv_typeMaster.Rows.RemoveAt(index);
}
/// <summary>
/// This method Validates the GridView on Sumbit.
/// </summary>
/// <param>
private bool validateGridView()
{
foreach (DataGridViewRow row in dgv_typeMaster.Rows)
{
if (row.Cells[1].Value == null || row.Cells[1].Value.ToString() == string.Empty)
{
MessageBox.Show("Enter Value of Type at Row " + (row.Index + 1), "Validation Failed");
row.Cells[1].Style.BackColor = Color.Red;
return false;
}
}
return true;
}
#endregion
#region Button Events
private void button1_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
if (validateGridView())
{
DataLayer.clearData("TypeMaster");
foreach (DataGridViewRow row in dgv_typeMaster.Rows)
{
DataLayer.InsertTypeMasterData(row);
}
PopulateGridView();
MessageBox.Show("Sucessfully Saved Data", "Success");
}
}
#endregion
#region Data Grid View Events
private void dgv_typeMaster_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
switch (e.ColumnIndex)
{
case 2: DisableEdit(dgv_typeMaster.CurrentCell.RowIndex, false); break;
case 3: DeleteRow(dgv_typeMaster.CurrentCell.RowIndex); break;
}
}
private void dgv_typeMaster_RowLeave(object sender, DataGridViewCellEventArgs e)
{
DisableEdit(e.RowIndex, true);
}
#endregion
}
}