This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
MainForm.cs
255 lines (234 loc) · 8.57 KB
/
MainForm.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
// Developer Express Code Central Example:
// How to implement CRUD operations using XtraGrid and WCF Data Services
//
// This example is similar to the http://www.devexpress.com/scid=E3866 example and
// demonstrates how to implement CRUD operations using XtraGrid and WCF Data
// Services.
//
// This example works with the standard SQL Northwind database.
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E4365
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Xpf.Core.ServerMode;
using DevExpress.Data.WcfLinq;
using System.Data.Services.Client;
using Example.ServiceReference1;
namespace Example {
public partial class MainForm : Form {
private NorthwindEntities context;
WcfServerModeSource wSMS;
private Customers newCustomer;
private Customers customerToEdit;
int OldRowCount = 0;
string newCustomerKey = String.Empty;
int SelectedRow = -1;
public MainForm() {
InitializeComponent();
context = new NorthwindEntities(new Uri(@"http://localhost:56848/NorthwindWcfDataService.svc/"));
wSMS = new WcfServerModeSource();
wSMS.KeyExpression += "CustomerID";
wSMS.Query = context.Customers;
gridControl1.DataSource = wSMS;
}
private void simpleButton1_Click(object sender, EventArgs e)
{
newCustomer = CreateNewCustomer();
OldRowCount = gridView1.DataRowCount;
newCustomerKey = newCustomer.CustomerID;
EditCustomer(newCustomer, "Add new customer", CloseAddNewCustomerHandler);
}
private void CloseAddNewCustomerHandler(object sender, EventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
context.AddToCustomers(newCustomer);
SaveChandes();
}
newCustomer = null;
}
private Customers CreateNewCustomer()
{
var newCustomer = new Customers();
newCustomer.CustomerID = GenerateCustomerID();
return newCustomer;
}
private string GenerateCustomerID()
{
const int IDLength = 5;
var result = String.Empty;
var rnd = new Random();
for (var i = 0; i < IDLength; i++)
{
result += Convert.ToChar(rnd.Next(65, 90));
}
return result;
}
private void EditCustomer(Customers customer, string windowTitle, FormClosingEventHandler closedDelegate)
{
var frm = new EditForm(customer);
frm.Text = windowTitle;
frm.FormClosing += closedDelegate;
frm.ShowDialog();
}
private void SaveChandes()
{
IAsyncResult asyncResult = null;
try
{
asyncResult = context.BeginSaveChanges(SaveCallback, null);
}
catch (Exception ex)
{
context.CancelRequest(asyncResult);
HandleException(ex);
}
}
private void SaveCallback(IAsyncResult asyncResult)
{
DataServiceResponse response = null;
try
{
response = context.EndSaveChanges(asyncResult);
}
catch (Exception ex)
{
gridControl1.BeginInvoke((MethodInvoker)delegate { context.CancelRequest(asyncResult); });
HandleException(ex);
gridControl1.BeginInvoke((MethodInvoker)delegate { DetachFailedEntities(); });
}
gridControl1.BeginInvoke((MethodInvoker)delegate { wSMS.Reload(); });
gridControl1.BeginInvoke((MethodInvoker)delegate {
if (SelectedRow > 0)
gridView1.FocusedRowHandle = SelectedRow;
if (!String.IsNullOrEmpty(newCustomerKey) && gridView1.DataRowCount > OldRowCount)
{
for (int i = 0; i < gridView1.DataRowCount; i++)
{
if (newCustomerKey == gridView1.GetRowCellValue(i, gridView1.Columns["CustomerID"]).ToString())
{
gridView1.FocusedRowHandle = i;
OldRowCount = gridView1.DataRowCount;
newCustomerKey = String.Empty;
break;
}
}
}
});
}
private void DetachFailedEntities()
{
foreach (EntityDescriptor entityDescriptor in context.Entities)
{
if (entityDescriptor.State != EntityStates.Unchanged)
{
context.Detach(entityDescriptor.Entity);
}
}
}
private void HandleException(Exception ex)
{
gridControl1.BeginInvoke((MethodInvoker)delegate { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK); });
}
private void simpleButton2_Click(object sender, EventArgs e)
{
SelectedRow = gridView1.FocusedRowHandle;
EditSelectedCustomer(gridView1.FocusedRowHandle);
}
private void EditSelectedCustomer(int rowHandle)
{
if (rowHandle < 0)
{
return;
}
FindCustomerByIDAndProcess(GetCustomerIDByRowHandle(rowHandle), customer =>
{
customerToEdit = customer;
EditCustomer(customerToEdit, "Edit customer", CloseEditCustomerHandler);
});
}
private string GetCustomerIDByRowHandle(int rowHandle)
{
return (string)gridView1.GetRowCellValue(rowHandle, "CustomerID");
}
private void CloseEditCustomerHandler(object sender, EventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
context.UpdateObject(customerToEdit);
SaveChandes();
}
customerToEdit = null;
}
private void FindCustomerByIDAndProcess(string customerID, Action<Customers> action)
{
var query = (DataServiceQuery<Customers>)context.Customers.Where<Customers>(customer => customer.CustomerID == customerID);
try
{
query.BeginExecute(FindCustomerByIDCallback, new QueryAction(query, action));
}
catch (Exception ex)
{
HandleException(ex);
}
}
private void FindCustomerByIDCallback(IAsyncResult ar)
{
var state = (QueryAction)ar.AsyncState;
var customers = state.Query.EndExecute(ar);
foreach (Customers customer in customers)
{
try
{
gridControl1.BeginInvoke((MethodInvoker)delegate { state.Action(customer); });
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
private void simpleButton3_Click(object sender, EventArgs e)
{
SelectedRow = gridView1.FocusedRowHandle;
DeleteSelectedCustomer(gridView1.FocusedRowHandle);
}
private void DeleteSelectedCustomer(int rowHandle)
{
if (rowHandle < 0)
{
return;
}
if (MessageBox.Show("Do you really want to delete the selected customer?", "Delete Customer", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
FindCustomerByIDAndProcess(GetCustomerIDByRowHandle(rowHandle), customer =>
{
context.DeleteObject(customer);
SaveChandes();
});
}
}
public class QueryAction {
private DataServiceQuery<Customers> query;
private Action<Customers> action;
public QueryAction(DataServiceQuery<Customers> query, Action<Customers> action) {
this.query = query;
this.action = action;
}
public DataServiceQuery<Customers> Query {
get { return query; }
}
public Action<Customers> Action {
get { return action; }
}
}
}