-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
218 lines (208 loc) · 7.55 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
// Developer Express Code Central Example:
// How to implement CRUD operations using XtraGrid and XPInstantFeedbackSource
//
// This example demonstrates how to implement the Create, Update and Delete
// operations using XPInstantFeedbackSource.
// 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=E4505
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
namespace XPInstantFeedback
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
gridView1.AsyncCompleted += new EventHandler(gridView1_AsyncCompleted);
gridView1.OptionsView.WaitAnimationOptions = WaitAnimationOptions.Panel;
gridControl.DataSource = xpInstantFeedbackSource1;
}
Customers customerToEdit;
EditForm f1;
private int oldRowsCount;
void gridView1_AsyncCompleted(object sender, EventArgs e)
{
if (customerToEdit != null && gridView1.DataRowCount > oldRowsCount)
{
for (int i = 0; i < gridView1.DataRowCount; i++)
{
if (customerToEdit.CustomerID == gridView1.GetRowCellValue(i, gridView1.Columns["CustomerID"]).ToString())
{
gridView1.FocusedRowHandle = i;
oldRowsCount = gridView1.DataRowCount;
customerToEdit = null;
break;
}
}
}
}
private void xpInstantFeedbackSource1_DismissSession(object sender, ResolveSessionEventArgs e)
{
IDisposable session1 = e.Session as IDisposable;
if (session1 != null)
{
session1.Dispose();
}
}
private void xpInstantFeedbackSource1_ResolveSession(object sender, ResolveSessionEventArgs e)
{
session1 = new Session() { ConnectionString = MSSqlConnectionProvider.GetConnectionString("(local)", "NorthWind") };
session1.Connect();
e.Session = session1;
}
private void button1_Click(object sender, EventArgs e)
{
session1.BeginTrackingChanges();
oldRowsCount = gridView1.DataRowCount;
customerToEdit = CreateCustomer();
EditCustomer(customerToEdit, "NewCustomer", CloseNewCustomerHandler);
}
private Customers CreateCustomer()
{
string idString;
var newCustomer = new Customers(session1);
while (true)
{
idString = GenerateCustomerID();
if (!String.IsNullOrEmpty(idString))
{
newCustomer.CustomerID = idString;
break;
}
}
return newCustomer;
}
private string GenerateCustomerID()
{
const int IDLength = 5;
var result = String.Empty;
var rnd = new Random();
bool collisionFlag = false;
for (var i = 0; i < IDLength; i++)
{
result += Convert.ToChar(rnd.Next(65, 90));
}
for (int i = 0; i <gridView1.DataRowCount; i++)
{
if (result == gridView1.GetRowCellValue(i, gridView1.Columns["CustomerID"]).ToString())
{
collisionFlag = true;
break;
}
}
if (collisionFlag)
{
return String.Empty;
}
else
return result;
}
private void button2_Click(object sender, EventArgs e)
{
session1.BeginTrackingChanges();
string key = GetCustomerIDByRowHandle(gridView1.FocusedRowHandle);
customerToEdit = session1.GetObjectByKey(typeof(Customers), key) as Customers;
EditCustomer(customerToEdit, "EditInfo", CloseEditCustomerHandler);
}
private void EditCustomer(Customers customer, string windowTitle, FormClosingEventHandler closedDelegate)
{
f1 = new EditForm(customer) { Text = windowTitle };
f1.FormClosing += closedDelegate;
f1.ShowDialog();
}
private string GetCustomerIDByRowHandle(int rowHandle)
{
return (string)gridView1.GetRowCellValue(rowHandle, "CustomerID");
}
private void CloseEditCustomerHandler(object sender, EventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
try
{
customerToEdit.Save();
session1.CommitTransaction();
xpInstantFeedbackSource1.Refresh();
}
catch (Exception ex)
{
HandleExcepton(ex);
}
}
customerToEdit = null;
}
private void CloseNewCustomerHandler(object sender, FormClosingEventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
try
{
customerToEdit.Save();
session1.CommitTransaction();
xpInstantFeedbackSource1.Refresh();
}
catch (Exception ex)
{
HandleExcepton(ex);
}
for (int i = 0; i < gridView1.DataRowCount; i++)
{
if (customerToEdit.CustomerID == gridView1.GetRowCellValue(i, gridView1.Columns["CustomerID"]).ToString())
{
gridView1.FocusedRowHandle = i;
break;
}
}
}
}
private void HandleExcepton(Exception ex)
{
MessageBox.Show(ex.Message);
}
private void button3_Click(object sender, EventArgs e)
{
session1.BeginTrackingChanges();
DeleteCustomer(gridView1.FocusedRowHandle);
}
private void DeleteCustomer(int focusedRowHandle)
{
if (focusedRowHandle < 0)
return;
if (MessageBox.Show("Do you really want to delete the selected customer?", "Delete Customer", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
Customers victim = session1.GetObjectByKey(typeof(Customers), GetCustomerIDByRowHandle(gridView1.FocusedRowHandle)) as Customers;
try
{
if (victim != null)
{
victim.Delete();
session1.CommitTransaction();
xpInstantFeedbackSource1.Refresh();
}
}
catch (Exception ex)
{
HandleExcepton(ex);
}
gridView1.FocusedRowHandle = focusedRowHandle;
customerToEdit = null;
}
}
}