/
Form1.vb
188 lines (161 loc) · 7.4 KB
/
Form1.vb
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
' 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
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.Skins
Imports DevExpress.XtraEditors
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Namespace XPInstantFeedback
Public Partial Class Form1
Inherits XtraForm
Public Sub New()
InitializeComponent()
AddHandler gridView1.AsyncCompleted, New EventHandler(AddressOf gridView1_AsyncCompleted)
gridView1.OptionsView.WaitAnimationOptions = WaitAnimationOptions.Panel
gridControl.DataSource = xpInstantFeedbackSource1
End Sub
Private customerToEdit As Customers
Private f1 As EditForm
Private oldRowsCount As Integer
Private Sub gridView1_AsyncCompleted(ByVal sender As Object, ByVal e As EventArgs)
If customerToEdit IsNot Nothing AndAlso gridView1.DataRowCount > oldRowsCount Then
For i As Integer = 0 To gridView1.DataRowCount - 1
If Equals(customerToEdit.CustomerID, gridView1.GetRowCellValue(i, gridView1.Columns("CustomerID")).ToString()) Then
gridView1.FocusedRowHandle = i
oldRowsCount = gridView1.DataRowCount
customerToEdit = Nothing
Exit For
End If
Next
End If
End Sub
Private Sub xpInstantFeedbackSource1_DismissSession(ByVal sender As Object, ByVal e As ResolveSessionEventArgs)
Dim session1 As IDisposable = TryCast(e.Session, IDisposable)
If session1 IsNot Nothing Then
session1.Dispose()
End If
End Sub
Private Sub xpInstantFeedbackSource1_ResolveSession(ByVal sender As Object, ByVal e As ResolveSessionEventArgs)
session1 = New Session() With {.ConnectionString = MSSqlConnectionProvider.GetConnectionString("(local)", "NorthWind")}
session1.Connect()
e.Session = session1
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
session1.BeginTrackingChanges()
oldRowsCount = gridView1.DataRowCount
customerToEdit = CreateCustomer()
EditCustomer(customerToEdit, "NewCustomer", New FormClosingEventHandler(AddressOf CloseNewCustomerHandler))
End Sub
Private Function CreateCustomer() As Customers
Dim idString As String
Dim newCustomer = New Customers(session1)
While True
idString = GenerateCustomerID()
If Not String.IsNullOrEmpty(idString) Then
newCustomer.CustomerID = idString
Exit While
End If
End While
Return newCustomer
End Function
Private Function GenerateCustomerID() As String
Const IDLength As Integer = 5
Dim result = String.Empty
Dim rnd = New Random()
Dim collisionFlag As Boolean = False
For i = 0 To IDLength - 1
result += Convert.ToChar(rnd.Next(65, 90))
Next
For i As Integer = 0 To gridView1.DataRowCount - 1
If Equals(result, gridView1.GetRowCellValue(i, gridView1.Columns("CustomerID")).ToString()) Then
collisionFlag = True
Exit For
End If
Next
If collisionFlag Then
Return String.Empty
Else
Return result
End If
End Function
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
session1.BeginTrackingChanges()
Dim key As String = GetCustomerIDByRowHandle(gridView1.FocusedRowHandle)
customerToEdit = TryCast(session1.GetObjectByKey(GetType(Customers), key), Customers)
EditCustomer(customerToEdit, "EditInfo", New FormClosingEventHandler(AddressOf CloseEditCustomerHandler))
End Sub
Private Sub EditCustomer(ByVal customer As Customers, ByVal windowTitle As String, ByVal closedDelegate As FormClosingEventHandler)
f1 = New EditForm(customer) With {.Text = windowTitle}
AddHandler f1.FormClosing, closedDelegate
f1.ShowDialog()
End Sub
Private Function GetCustomerIDByRowHandle(ByVal rowHandle As Integer) As String
Return CStr(gridView1.GetRowCellValue(rowHandle, "CustomerID"))
End Function
Private Sub CloseEditCustomerHandler(ByVal sender As Object, ByVal e As EventArgs)
If CType(sender, EditForm).DialogResult = DialogResult.OK Then
Try
customerToEdit.Save()
session1.CommitTransaction()
xpInstantFeedbackSource1.Refresh()
Catch ex As Exception
HandleExcepton(ex)
End Try
End If
customerToEdit = Nothing
End Sub
Private Sub CloseNewCustomerHandler(ByVal sender As Object, ByVal e As FormClosingEventArgs)
If CType(sender, EditForm).DialogResult = DialogResult.OK Then
Try
customerToEdit.Save()
session1.CommitTransaction()
xpInstantFeedbackSource1.Refresh()
Catch ex As Exception
HandleExcepton(ex)
End Try
For i As Integer = 0 To gridView1.DataRowCount - 1
If Equals(customerToEdit.CustomerID, gridView1.GetRowCellValue(i, gridView1.Columns("CustomerID")).ToString()) Then
gridView1.FocusedRowHandle = i
Exit For
End If
Next
End If
End Sub
Private Sub HandleExcepton(ByVal ex As Exception)
MessageBox.Show(ex.Message)
End Sub
Private Sub button3_Click(ByVal sender As Object, ByVal e As EventArgs)
session1.BeginTrackingChanges()
DeleteCustomer(gridView1.FocusedRowHandle)
End Sub
Private Sub DeleteCustomer(ByVal focusedRowHandle As Integer)
If focusedRowHandle < 0 Then Return
If MessageBox.Show("Do you really want to delete the selected customer?", "Delete Customer", MessageBoxButtons.OKCancel) <> DialogResult.OK Then
Return
End If
Dim victim As Customers = TryCast(session1.GetObjectByKey(GetType(Customers), GetCustomerIDByRowHandle(gridView1.FocusedRowHandle)), Customers)
Try
If victim IsNot Nothing Then
victim.Delete()
session1.CommitTransaction()
xpInstantFeedbackSource1.Refresh()
End If
Catch ex As Exception
HandleExcepton(ex)
End Try
gridView1.FocusedRowHandle = focusedRowHandle
customerToEdit = Nothing
End Sub
End Class
End Namespace