-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path
235 lines (233 loc) · 12.6 KB
/
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
[33mcommit c36336e44b2eb100f42742dae67001b4efd62250[m[33m ([m[1;36mHEAD -> [m[1;32mmaster[m[33m, [m[1;31morigin/master[m[33m)[m
Author: Karen Payne <[email protected]>
Date: Mon Oct 26 19:45:24 2020 -0700
Code to remove record in UserDeletingRow event for a DataGridView.
[1mdiff --git a/North/Classes/CustomerEntity.cs b/North/Classes/CustomerEntity.cs[m
[1mindex 7c7b783..a064578 100644[m
[1m--- a/North/Classes/CustomerEntity.cs[m
[1m+++ b/North/Classes/CustomerEntity.cs[m
[36m@@ -155,6 +155,11 @@[m [mnamespace North.Classes[m
}[m
}[m
[m
[32m+[m[32m public override string ToString()[m
[32m+[m[32m {[m
[32m+[m[32m return CustomerIdentifier.ToString();[m
[32m+[m[32m }[m
[32m+[m
public event PropertyChangedEventHandler PropertyChanged;[m
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)[m
{[m
[1mdiff --git a/North/Forms/CustomersDataGridViewTestForm.Designer.cs b/North/Forms/CustomersDataGridViewTestForm.Designer.cs[m
[1mindex f57dd1f..e1fd0e5 100644[m
[1m--- a/North/Forms/CustomersDataGridViewTestForm.Designer.cs[m
[1m+++ b/North/Forms/CustomersDataGridViewTestForm.Designer.cs[m
[36m@@ -31,6 +31,8 @@[m
this.components = new System.ComponentModel.Container();[m
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CustomersDataGridViewTestForm));[m
this.panel1 = new System.Windows.Forms.Panel();[m
[32m+[m[32m this.label1 = new System.Windows.Forms.Label();[m
[32m+[m[32m this.AddNewCustomerButton = new System.Windows.Forms.Button();[m
this.CompanyNameStartsWithTextBox = new System.Windows.Forms.TextBox();[m
this.SaveChangesButton = new System.Windows.Forms.Button();[m
this.CurrentCustomerDetails = new System.Windows.Forms.Button();[m
[36m@@ -64,6 +66,8 @@[m
// [m
// panel1[m
// [m
[32m+[m[32m this.panel1.Controls.Add(this.label1);[m
[32m+[m[32m this.panel1.Controls.Add(this.AddNewCustomerButton);[m
this.panel1.Controls.Add(this.CompanyNameStartsWithTextBox);[m
this.panel1.Controls.Add(this.SaveChangesButton);[m
this.panel1.Controls.Add(this.CurrentCustomerDetails);[m
[36m@@ -73,6 +77,25 @@[m
this.panel1.Size = new System.Drawing.Size(1060, 56);[m
this.panel1.TabIndex = 0;[m
// [m
[32m+[m[32m // label1[m
[32m+[m[32m //[m[41m [m
[32m+[m[32m this.label1.AutoSize = true;[m
[32m+[m[32m this.label1.Location = new System.Drawing.Point(456, 24);[m
[32m+[m[32m this.label1.Name = "label1";[m
[32m+[m[32m this.label1.Size = new System.Drawing.Size(35, 13);[m
[32m+[m[32m this.label1.TabIndex = 4;[m
[32m+[m[32m this.label1.Text = "label1";[m
[32m+[m[32m //[m[41m [m
[32m+[m[32m // AddNewCustomerButton[m
[32m+[m[32m //[m[41m [m
[32m+[m[32m this.AddNewCustomerButton.Location = new System.Drawing.Point(303, 19);[m
[32m+[m[32m this.AddNewCustomerButton.Name = "AddNewCustomerButton";[m
[32m+[m[32m this.AddNewCustomerButton.Size = new System.Drawing.Size(129, 23);[m
[32m+[m[32m this.AddNewCustomerButton.TabIndex = 3;[m
[32m+[m[32m this.AddNewCustomerButton.Text = "Add code";[m
[32m+[m[32m this.AddNewCustomerButton.UseVisualStyleBackColor = true;[m
[32m+[m[32m this.AddNewCustomerButton.Click += new System.EventHandler(this.AddNewCustomerButton_Click);[m
[32m+[m[32m //[m[41m [m
// CompanyNameStartsWithTextBox[m
// [m
this.CompanyNameStartsWithTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));[m
[36m@@ -279,6 +302,7 @@[m
// DeleteCustomerBindingNavigatorButton[m
// [m
this.DeleteCustomerBindingNavigatorButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;[m
[32m+[m[32m this.DeleteCustomerBindingNavigatorButton.Enabled = false;[m
this.DeleteCustomerBindingNavigatorButton.Image = ((System.Drawing.Image)(resources.GetObject("DeleteCustomerBindingNavigatorButton.Image")));[m
this.DeleteCustomerBindingNavigatorButton.Name = "DeleteCustomerBindingNavigatorButton";[m
this.DeleteCustomerBindingNavigatorButton.RightToLeftAutoMirrorImage = true;[m
[36m@@ -340,5 +364,7 @@[m
private System.Windows.Forms.Button SaveChangesButton;[m
private System.Windows.Forms.ToolTip toolTip1;[m
private System.Windows.Forms.TextBox CompanyNameStartsWithTextBox;[m
[32m+[m[32m private System.Windows.Forms.Button AddNewCustomerButton;[m
[32m+[m[32m private System.Windows.Forms.Label label1;[m
}[m
}[m
\ No newline at end of file[m
[1mdiff --git a/North/Forms/CustomersDataGridViewTestForm.cs b/North/Forms/CustomersDataGridViewTestForm.cs[m
[1mindex 8b9d081..5cc147c 100644[m
[1m--- a/North/Forms/CustomersDataGridViewTestForm.cs[m
[1m+++ b/North/Forms/CustomersDataGridViewTestForm.cs[m
[36m@@ -8,6 +8,7 @@[m [musing System.Text;[m
using System.Threading.Tasks;[m
using System.Windows.Forms;[m
using LanguageExtensions;[m
[32m+[m[32musing Microsoft.EntityFrameworkCore;[m
using Microsoft.EntityFrameworkCore.ChangeTracking;[m
using North.Classes;[m
using North.Classes.Components;[m
[36m@@ -24,6 +25,7 @@[m [mnamespace North.Forms[m
private SortableBindingList<CustomerEntity> _customerView;[m
private SortableBindingList<CustomerEntity> _customerViewFilter;[m
private readonly BindingSource _customerBindingSource = new BindingSource();[m
[32m+[m
private bool _filtered = false;[m
[m
public CustomersDataGridViewTestForm()[m
[36m@@ -53,6 +55,7 @@[m [mnamespace North.Forms[m
{[m
try[m
{[m
[32m+[m
var filter = CompanyNameStartsWithTextBox.Text.Trim();[m
_customerViewFilter = new SortableBindingList<CustomerEntity>(_customerView.Where(customerEntity =>[m
customerEntity.CompanyName.ToLower().StartsWith(filter)).ToList());[m
[36m@@ -99,8 +102,75 @@[m [mnamespace North.Forms[m
[m
CustomersDataGridView.CellValueChanged += CustomersDataGridView_CellValueChanged;[m
CustomersDataGridView.UserDeletingRow += CustomersDataGridView_UserDeletingRow;[m
[32m+[m
[32m+[m[41m [m
_customerView.ListChanged += _customerView_ListChanged;[m
[32m+[m[32m _customerView.AddingNew += _customerView_AddingNew;[m
[32m+[m
[32m+[m[32m DeleteCustomerBindingNavigatorButton.Enabled = true;[m
[32m+[m
[32m+[m[32m CustomersDataGridView.RowPrePaint += CustomersDataGridView_RowPrePaint;[m
[32m+[m[41m [m
[32m+[m
[32m+[m[32m }[m
[32m+[m[32m /// <summary>[m
[32m+[m[32m /// - Set default values (hard coded) for new row.[m
[32m+[m[32m /// - The code in this form was never meant to allow[m
[32m+[m[32m /// adding via the DataGridView, this is to show in part what to do.[m
[32m+[m[32m ///[m
[32m+[m[32m /// What needs to change? for one, the DataGridViewComboBoxes would[m
[32m+[m[32m /// need "Select" selection and code to validate that selection is[m
[32m+[m[32m /// not selected when leaving the row.[m
[32m+[m[32m /// </summary>[m
[32m+[m[32m /// <param name="sender"></param>[m
[32m+[m[32m /// <param name="e"></param>[m
[32m+[m[32m private void _customerView_AddingNew(object sender, AddingNewEventArgs e)[m
[32m+[m[32m {[m
[32m+[m[32m e.NewObject = new CustomerEntity() {ContactIdentifier = 91, CountryIdentifier = 9, ContactTypeIdentifier = 11};[m
[32m+[m[32m }[m
[32m+[m[32m /// <summary>[m
[32m+[m[32m /// Get newly added Customer (CustomerEntity)[m
[32m+[m[32m /// </summary>[m
[32m+[m[32m /// <param name="sender"></param>[m
[32m+[m[32m /// <param name="e"></param>[m
[32m+[m[32m private void CustomersDataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)[m
[32m+[m[32m {[m
[32m+[m[32m if (CustomersDataGridView.Rows[e.RowIndex].DataBoundItem != null)[m
[32m+[m[32m {[m
[32m+[m[32m label1.Text = ((CustomerEntity) CustomersDataGridView.Rows[e.RowIndex].DataBoundItem).CompanyName;[m
[32m+[m[32m }[m
[32m+[m[32m }[m
[32m+[m[32m /// <summary>[m
[32m+[m[32m /// For validating stuff, currently not production ready.[m
[32m+[m[32m /// </summary>[m
[32m+[m[32m /// <param name="sender"></param>[m
[32m+[m[32m /// <param name="e"></param>[m
[32m+[m[32m private void AddNewCustomerButton_Click(object sender, EventArgs e)[m
[32m+[m[32m {[m
[32m+[m[41m [m
[32m+[m[32m /*[m
[32m+[m[32m * NOTE: WIP[m
[32m+[m[32m */[m
[32m+[m
[32m+[m[32m CustomerEntity customerEntity = _customerView.FirstOrDefault(x => x.CustomerIdentifier == 0);[m
[32m+[m[32m customerEntity.ContactIdentifier = 52;[m
[32m+[m[32m customerEntity.CountryIdentifier = 9;[m
[m
[32m+[m[32m Customers customers = new Customers[m
[32m+[m[32m {[m
[32m+[m[32m CompanyName = customerEntity.CompanyName,[m
[32m+[m[32m CountryIdentifier = customerEntity.CountryIdentifier,[m
[32m+[m[32m ContactId = 52,[m
[32m+[m[32m ContactTypeIdentifier = 5[m
[32m+[m[32m };[m
[32m+[m
[32m+[m
[32m+[m[32m CustomersTestOperations.Context.Attach(customers).State = EntityState.Added;[m
[32m+[m[32m if (CustomersTestOperations.Context.SaveChanges() != 1)[m
[32m+[m[32m {[m
[32m+[m[32m // alert user and/or write to log file.[m
[32m+[m[32m }[m
[32m+[m[41m [m
}[m
[m
private bool _hasValidationErrors;[m
[36m@@ -120,7 +190,7 @@[m [mnamespace North.Forms[m
// Get current customer in current DataGridView row[m
//[m
CustomerEntity currentCustomer = _customerView.CurrentCustomer(_customerBindingSource.Position);[m
[31m-[m
[32m+[m[32m Console.WriteLine($" -> {e.ListChangedType.ToString()}");[m
//[m
// Assert if there are changes to the current row in the DataGridView[m
//[m
[36m@@ -303,17 +373,31 @@[m [mnamespace North.Forms[m
{[m
if (_customerView.Count <= 0) return;[m
[m
[31m- CustomerEntity customer = _customerView.CurrentCustomer(_customerBindingSource.Position);[m
[32m+[m[32m var customer = _customerView.CurrentCustomer(_customerBindingSource.Position);[m
[m
if (Question($"Remove {customer.CompanyName}"))[m
{[m
[31m- MessageBox.Show(@"Removal goes here");[m
[32m+[m
[32m+[m[32m var customerToRemove = CustomersTestOperations.Context.Customers.Find(customer.CustomerIdentifier);[m
[32m+[m[32m CustomersTestOperations.Context.Customers.Remove(customerToRemove);[m
[32m+[m[32m try[m
[32m+[m[32m {[m
[32m+[m[32m CustomersTestOperations.Context.SaveChanges();[m
[32m+[m[32m }[m
[32m+[m[32m catch (Exception ex)[m
[32m+[m[32m {[m
[32m+[m[32m /*[m
[32m+[m[32m * Common reason is no cascade rule to related table(s)[m
[32m+[m[32m */[m
[32m+[m[32m MessageBox.Show($"Failed to remove customer\n{ex.Message}");[m
[32m+[m[32m }[m
[32m+[m
}[m
else[m
{[m
e.Cancel = true;[m
[31m- MessageBox.Show(@"Removal aborted");[m
}[m
}[m
[32m+[m
}[m
}[m