-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
339 lines (300 loc) · 14.2 KB
/
Form1.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevexpressTreeListExample.Models;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DevexpressTreeListExample
{
public partial class Form1 : Form
{
private int fontSizeDeltaControl = 0;
public Form1()
{
InitializeComponent();
treeListFill();
LookUpEditFill();
}
#region Crud Operations
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (Convert.ToInt32(lookUpEditUserType.EditValue) > 0)
{
#region Önce Kulanıcı Tipi Silme işlemi
using (DatabaseContext context = new DatabaseContext())
{
int _selectedId = Convert.ToInt32(lookUpEditUserType.EditValue);
var dataRemoveList = context.CategoryUserTypes.Where(x => x.UserTypeID == _selectedId).ToList();
foreach (var item in dataRemoveList.ToList())
{
var list = context.CategoryUserTypes.FirstOrDefault(x => x.Id == item.Id);
context.CategoryUserTypes.Remove(list);
context.SaveChanges();
}
}
#endregion Önce Kulanıcı Tipi Silme işlemi
#region Kaydetme İşlemi
using (DatabaseContext db = new DatabaseContext())
{
foreach (TreeListNode item in treeList.Nodes)
{
string name = item.GetDisplayText("CategoryName");
Category _category = db.Categories.FirstOrDefault(x => x.CategoryName == name);
if (_category.CategoryName == item.GetDisplayText("CategoryName"))
{
if (item.CheckState == CheckState.Indeterminate)
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = true,
});
db.SaveChanges();
if (item.HasChildren)
GetChildNode(item, _category);
}
else
{
if ((bool)item.GetValue("Checked"))
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = true,
});
db.SaveChanges();
if (item.HasChildren)
GetChildNode(item, _category);
}
else
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = false,
});
db.SaveChanges();
if (item.HasChildren)
GetChildNode(item, _category);
}
}
}
}
}
#endregion Kaydetme İşlemi
MessageBox.Show("Kategoriler Başarılı Bir Şekilde Eklendi !");
}
else
{
MessageBox.Show("Lütfen Kullanıcı Tipi Seçiniz !");
}
treeList1Fill(Convert.ToInt32(lookUpEditUserType.EditValue));
}
catch (Exception ex)
{
MessageBox.Show("Hata Oluştu ! \nHata Mesajı: " + ex.ToString());
}
}
private void GetChildNode(TreeListNode ChildNode, Category category)
{
//Alt Kategorileri kayıt ediyor.
using (DatabaseContext db = new DatabaseContext())
{
foreach (TreeListNode item2 in ChildNode.Nodes)
{
string name = item2.GetDisplayText("CategoryName");
Category _category = db.Categories.FirstOrDefault(x => x.CategoryName == name);
if (item2.CheckState == CheckState.Indeterminate)
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = true,
});
db.SaveChanges();
if (item2.HasChildren)
GetChildNode(item2, _category);
}
else
{
if ((bool)item2.GetValue("Checked"))
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
SubCategoryID = (int)_category.SubCategoryID,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = true,
});
db.SaveChanges();
}
else
{
db.CategoryUserTypes.Add(new CategoryUserType
{
CategoryID = _category.Id,
SubCategoryID = (int)_category.SubCategoryID,
UserTypeID = Convert.ToInt32(lookUpEditUserType.EditValue),
IsChecked = false,
});
db.SaveChanges();
}
if (item2.HasChildren)
GetChildNode(item2, _category);
}
}
}
}
private void btnListRefresh_Click(object sender, EventArgs e)
{
treeListFill();
lookUpEditUserType.EditValue = null;
}
private void lookUpEditUserType_EditValueChanged(object sender, EventArgs e)
{
treeList1Fill(Convert.ToInt32(lookUpEditUserType.EditValue));
if (Convert.ToInt32(lookUpEditUserType.EditValue) > 0)
{
groupControl3.Text = "Kayıtlı Kategori Listesi (Tree List)" + " / " + lookUpEditUserType.Text;
}
else
{
groupControl3.Text = "Kayıtlı Kategori Listesi (Tree List)";
}
}
private void OnBeforeFocusNode(object sender, BeforeFocusNodeEventArgs e)
{
e.CanFocus = false;
}
private void OnNodeCellStyle(object sender, GetCustomNodeCellStyleEventArgs e)
{
//Ana Kategorileri Bold olarak fontu düzenliyor.
if (e.Node.Level == 0)
{
if (fontSizeDeltaControl == 0)
{
e.Appearance.FontSizeDelta += 1;
fontSizeDeltaControl++;
}
e.Appearance.FontStyleDelta = FontStyle.Bold;
}
if (e.Node.Level == 1 && e.Node.Nodes.Count > 0)
e.Appearance.FontStyleDelta = FontStyle.Bold;
}
#endregion Crud Operations
#region Methods
private void LookUpEditFill()
{
using (DatabaseContext db = new DatabaseContext())
{
var comboboxUserTypeList = db.UserTypes.Select(x => new ComboboxViewModel()
{
Id = x.Id.ToString(),
Name = x.UserTypeName,
}).ToList();
lookUpEditUserType.Properties.ValueMember = "Id";
lookUpEditUserType.Properties.DisplayMember = "Name";
lookUpEditUserType.Properties.NullText = "Lütfen Seçim Yapınız";
lookUpEditUserType.Properties.DataSource = comboboxUserTypeList.ToList();
}
}
private void treeListFill()
{
using (DatabaseContext db = new DatabaseContext())
{
treeList.Appearance.Row.BackColor = Color.Transparent;
treeList.Appearance.Empty.BackColor = Color.Transparent;
treeList.BackColor = Color.Transparent;
treeList.KeyFieldName = "Id";
treeList.ParentFieldName = "SubCategoryID";
treeList.CheckBoxFieldName = "Checked";
treeList.TreeViewFieldName = "CategoryName";
treeList.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
treeList.OptionsBehavior.Editable = false;
treeList.OptionsBehavior.ReadOnly = true;
treeList.OptionsBehavior.AllowRecursiveNodeChecking = true;
treeList.NodeCellStyle += OnNodeCellStyle;
treeList.BeforeFocusNode += OnBeforeFocusNode;
treeList.DataSource = db.Categories.Select(x => new CategoryViewModel
{
Id = x.Id,
CategoryName = x.CategoryName,
SubCategoryID = x.SubCategoryID,
Checked = true,
}).ToList();
treeList.ForceInitialize();
treeList.OptionsView.RootCheckBoxStyle = NodeCheckBoxStyle.Check;
//Tüm kategoriye checkbox ekliyor.
treeList.OptionsView.CheckBoxStyle = DefaultNodeCheckBoxStyle.Check;
#region Manuel Chechbox Created
//Nodes[1]
// treeList.Nodes[1].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[1].Nodes[0]
// treeList.Nodes[1].Nodes[0].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[1].Nodes[1]
// treeList.Nodes[1].Nodes[1].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[1].Nodes[1].Nodes[0]
//treeList.Nodes[1].Nodes[1].Nodes[0].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[1].Nodes[1].Nodes[1]
//treeList.Nodes[1].Nodes[1].Nodes[1].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[2]
// treeList.Nodes[2].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[2].Nodes[0]
// treeList.Nodes[2].Nodes[0].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[2].Nodes[1]
// treeList.Nodes[2].Nodes[1].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
//treeList.Nodes[2].Nodes[2]
//treeList.Nodes[2].Nodes[2].ChildrenCheckBoxStyle = NodeCheckBoxStyle.Check;
#endregion Manuel Chechbox Created
treeList.ExpandAll();
}
}
private void treeList1Fill(int id)
{
try
{
using (DatabaseContext db = new DatabaseContext())
{
treeList1.Appearance.Row.BackColor = Color.Transparent;
treeList1.Appearance.Empty.BackColor = Color.Transparent;
treeList1.BackColor = Color.Transparent;
treeList1.KeyFieldName = "Id";
treeList1.ParentFieldName = "SubCategoryID";
treeList1.TreeViewFieldName = "CategoryName";
treeList1.RootValue = "0";
treeList1.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
treeList1.OptionsBehavior.Editable = false;
treeList1.OptionsBehavior.ReadOnly = true;
treeList1.OptionsBehavior.AllowRecursiveNodeChecking = true;
treeList1.NodeCellStyle += OnNodeCellStyle;
treeList1.BeforeFocusNode += OnBeforeFocusNode;
var result = (from categoryUserTypes in db.CategoryUserTypes
join category in db.Categories on categoryUserTypes.CategoryID equals category.Id
join userType in db.UserTypes on categoryUserTypes.UserTypeID equals userType.Id
where categoryUserTypes.UserTypeID == id && categoryUserTypes.IsChecked == true
select new CategoryViewModel
{
Id = categoryUserTypes.CategoryID,
CategoryName = category.CategoryName,
SubCategoryID = categoryUserTypes.SubCategoryID,
}).ToList();
treeList1.DataSource = result;
treeList1.ForceInitialize();
treeList1.ExpandAll();
}
}
catch (Exception)
{
}
}
#endregion Methods
}
}