Hi,
Use the dataadapters rowupdating event. My example deletes a
row from a datatable and cancels it from being sent to the database.
http://msdn.microsoft.com/library/de...atingtopic.asp http://msdn.microsoft.com/library/de...atingtopic.asp
Dim WithEvents ds As New DataSet
Dim WithEvents da As OleDbDataAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim conn As OleDbConnection
strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source = Northwind.mdb;"
conn = New OleDbConnection(strConn)
da = New OleDbDataAdapter("Select * From Categories", conn)
da.Fill(ds, "Categories")
DataGrid1.DataSource = ds .Tables("Categories")
ds.Tables("Categories").Rows(0).Delete()
da.Update(ds, "Categories")
End Sub
Private Sub da_RowUpdating(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatingEventArgs) Handles da.RowUpdating
MessageBox.Show(e.StatementType.ToString)
e.Status = UpdateStatus.SkipCurrentRow
End Sub
Private Sub da_RowUpdated(ByVal sender As Object, ByVal e As
System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles da.RowUpdated
MessageBox.Show("Deleted")
End Sub
Ken
-------------------------------------
"mttc" <s@hotmail.com> wrote in message news:41********@news.012.net.il...
Thanks all
I see that this issue is interesting all developers. By the way why the
Microsoft deals with this need directly?
Ok, let's clear my q, I talk about Prevent delete, but that not exactly my
target.
I need to give the user tow way to delete from the grid, by Del key and by
Button.
I want to give user ability to cancel or confirm by MsgBox. And I want to
Update
DB immediately
The problam is that RowDeleting event not comes with:
ByRef Cancel parameter as Boolean
(This basic parameter missing in many event in Dot.Net like KeyPress)
we can resolve this problem in many ways:
A. Drive new class of DataGrid like this:
http://www.onteorasoftware.com/downloads/delete.zip
B. Drive new class of DataTable with new version of RowDeleteng,RowChangin
that include:
ByRef Cancel parameter as Boolean.
C. Throwing exception within the RowDeleted event (I find it in some
microsoft article)
D. Use with RejectChange. This way is problematical. You need to avoid
deadlock.
If you call Adepter.update from within the events, you should know, that
calls to
AcceptChange, that call again to RowDeleted. heare compleate code in
shortly:
===============
Write in class:
Dim ConfirmDelete As Boolean = False
Write in form Load
AddHandler Ds.Tables("xxx").RowDeleted, AddressOf OnDel
AddHandler DsTorm.Tables("xxx").RowDeleting, AddressOf OnDeleting
Sub OnDeleting(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If MsgBox(msg,MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then
ConfirmDelete = True
Else
ConfirmDelete = False
End If
End Sub
Sub OnDel (ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs)
If e.Action = DataRowAction.Commit Then Exit Sub
If (Not ConfirmDelete) Then
e.Row.RejectChanges()
Exit Sub
End If
ConfirmDelete = False
Try
Dim s As String = "delete from tikshoret where [id]=" & e.Row("id",
DataRowVersion.Original)
Dim c As New SqlCommand(s, Conn)
c.ExecuteNonQuery()
e.Row.AcceptChanges()
Catch ex As Exception
e.Row.RejectChanges()
End Try
================
Now my q is if I go with C solution (see above) where I can catch this
exception.
In other word I can ask:
Where I can catch Exception that arise with built in control-responds?