Public Class MyDeleteGridEventArgs
Inherits EventArgs
Private _IsError As Boolean = False
Private _Ex As Exception = Nothing
Public Property IsError() As Boolean
Get
Return _IsError
End Get
Set
_IsError = value
End Set
End Property
Public Property Ex() As Exception
Get
Return _Ex
End Get
Set
_Ex = value
_IsError = (value IsNot Nothing)
End Set
End Property
Public Sub New(argIsError As Boolean)
_IsError = argIsError
End Sub
Public Sub New(argEx As Exception)
Me.Ex = argEx
End Sub
End Class
Class ....
Delegate Sub OnGridRowDeleteHandler ( ByVal Sender As Object ,ByVal e As MyDeleteGridEventArgs )
Public Event OnGridRowDeleteCancel AS OnGridRowDeleteHandler
...
...
...
Private Sub dt_RowDelete( ByVal Sender As Object ,ByVal e As DataRowChangedEvent ) Handles dt.RowDeleted
if CancelAction Then
CancelAction = False
e.Row.RejectChanges()
Dim ex As Exception = New Exception ("Has account transaction.")
Dim myEventArgs As MyDeleteGridEventArgs = New MyDeleteGridEventArgs (ex)
RaiseEvent OnGridRowDeleteCancel ( Sender ,myEventArgs )
End If
End Suub
...
...
...
End Class
' ไม่ได้ผิดรูปแบบ microsoft อะรัยนี่คะ เพราะทั้ง .net framework เกือบจะทุก class เขียนมาให้ inherit อยู่ละ
' ถ้าจะเอาไปใช้ ก็ไปเชื่อม event ด้วยการ handle ต่อไปเรื่อยๆ ค่ะ
Class ....
Delegate Sub OnGridRowDeleteHandler ( ByVal Sender As Object ,ByVal e As MyDeleteGridEventArgs )
Public Event OnGridRowDeleteCancel AS OnGridRowDeleteHandler
...
...
...
Private Sub dt_RowDelete( ByVal Sender As Object ,ByVal e As DataRowChangedEvent ) Handles dt.RowDeleted
If CancelAction Then
CancelAction = False
e.Row.RejectChanges()
Dim ex As Exception = New Exception ("Has account transaction.")
Dim myEventArgs As MyDeleteGridEventArgs = New MyDeleteGridEventArgs (ex)
RaiseEvent OnGridRowDeleteCancel ( Sender ,myEventArgs )
End If
End Sub
...
...
...
End Class
กับแบบไม่ต้องประกาศ Delegate Sub มันมีความแตกต่างในด้านประสิทธิภาพ หรือเรื่องไหนรึเปล่าครับ Code (VB.NET)
Class ....
Public Event OnGridRowDeleteCancel ( ByVal Sender As Object ,ByVal e As MyDeleteGridEventArgs )
...
...
...
Private Sub dt_RowDelete( ByVal Sender As Object ,ByVal e As DataRowChangedEvent ) Handles dt.RowDeleted
If CancelAction Then
CancelAction = False
e.Row.RejectChanges()
Dim ex As Exception = New Exception ("Has account transaction.")
Dim myEventArgs As MyDeleteGridEventArgs = New MyDeleteGridEventArgs (ex)
RaiseEvent OnGridRowDeleteCancel ( Sender ,myEventArgs )
End If
End Sub
...
...
...
End Class
Public Class MyDeleteGridEventArgs
Private _AllowDeleted As Exception = Nothing
Private _DeleteAcquirorName As String = String.Empty
Public Property AllowDeleted() As Boolean
Get
Return _AllowDeleted
End Get
Set
_AllowDeleted = value
End Set
End Property
'Relax checking with no case-sensitive
Public Property _DeleteAcquirorName () As String
Get
Return _DeleteAcquirorName
End Get
Set
_DeleteAcquirorName = value.ToUpper()
End Set
End Property
'Basic Constructor
Public Sub New ()
End Sub
'Take Parameter Constructor
Public Sub New( ByVal varAllowDeleted As Boolean, ByVal varDeleteAcquirorName As String )
_DeleteAcquirorName = varDeleteAcquirorName
_AllowDeleted = varAllowDeleted
End Sub
End Class
Class ....
Delegate Sub DeleteAccountDeleteHandler ( ByVal Sender As Object ,ByVal e As MyDeleteGridEventArgs )
Public Overridable Sub BeginDeleteAccountDataHandler ( ByVal Sender As Object ,ByVal e As MyDeleteGridEventArgs )
Select Case e.DeleteAcquirorName
Case "MAINGRID" :
' Check Bussiness rules with MAINGRIDaccquire
' If valid Set AllowDeleted = true
' Suppose custom function name CheckedDeletableWithBussinessRuleWithMainGrid
e.AllowDeleted = CheckedDeletableWithBussinessRuleWithMainGrid()
Case Else
' Suppose custom function name CheckedDeletableWithBussinessRuleByDefault
e.AllowDeleted = CheckedDeletableWithBussinessRuleByDefault()
End Select
End Sub
...
...
...
Private Sub dt_RowDelete( ByVal Sender As Object ,ByVal e As DataRowChangedEvent ) Handles dt.RowDeleted
Dim myEventArgs As MyDeleteGridEventArgs = New MyDeleteGridEventArgs ()
myEventArgs.DeleteAcquirorName = "MAINGRID"
BeginDeleteAccountDataHandler ( myEventArgs )
'Bypass delete with bussiness logic
If (Not myEventArgs.AllowDeleted ) Then
e.Row.RejectChanges()
End if
End Sub
...
...
...
End Class