DataRowChangeEvents

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a question about these events that I hope someone could help me with.

I have a DataGrid bound to a DataSet. The grid's datasource is a DataView
and deletes are allowed.

I have added a Event Handler for OnRowDeleting.

What I want to do, is to only allow deletes to occur for newly added records
(before DataSet.Update is run). I want to cancel deletes for existing
records.

The code for OnRowDeleting is:

Private Sub OnRowDeleting(ByVal sender As Object, ByVal e As
DataRowChangeEventArgs)
'=============================================================
' This event fires when a row is being deleted
|
'=============================================================
Try
If e.Row.RowState <> DataRowState.Added Then
Throw (New DeleteAgencyException("Cannot Delete an Existing
Row"))
End If
Catch ex As DeleteAgencyException
MessageBox.Show(ex.Message, "Delete Agency")
e.Row.RejectChanges()
End Try
End Sub

What happens is my Exception is captured and the message displayed, but the
row is still deleted. The RejectChanges doesn't seem to actually reject the
changes. I tried CancelEdit and got the same results. How can I cancel the
changes?

Thanks
John
 
Hi,

You need to create an inherited datagrid for that. Here is the code
for one that will raise an event to let you the user is about to delete a
row. Also prevents the user from resizing a row or column.

Public Class ConfirmDeleteDataGrid

Inherits DataGrid

Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)

Private Const WM_KEYDOWN = &H100



Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean

Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)

If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Dim pt As Point

Dim hti As DataGrid.HitTestInfo

pt = Me.PointToClient(Cursor.Position)

hti = Me.HitTest(pt)

If keyData = Keys.Delete Then

If hti.Type = Me.HitTestType.RowHeader Then

If MessageBox.Show("Delete this row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function



Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)

End Sub

Public Sub New()

Trace.WriteLine(Me.VertScrollBar.Visible.ToString)

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)

End Sub


End Class



Ken

-------------------------------


I have a question about these events that I hope someone could help me with.

I have a DataGrid bound to a DataSet. The grid's datasource is a DataView
and deletes are allowed.

I have added a Event Handler for OnRowDeleting.

What I want to do, is to only allow deletes to occur for newly added records
(before DataSet.Update is run). I want to cancel deletes for existing
records.

The code for OnRowDeleting is:

Private Sub OnRowDeleting(ByVal sender As Object, ByVal e As
DataRowChangeEventArgs)
'=============================================================
' This event fires when a row is being deleted
|
'=============================================================
Try
If e.Row.RowState <> DataRowState.Added Then
Throw (New DeleteAgencyException("Cannot Delete an Existing
Row"))
End If
Catch ex As DeleteAgencyException
MessageBox.Show(ex.Message, "Delete Agency")
e.Row.RejectChanges()
End Try
End Sub

What happens is my Exception is captured and the message displayed, but the
row is still deleted. The RejectChanges doesn't seem to actually reject the
changes. I tried CancelEdit and got the same results. How can I cancel the
changes?

Thanks
John
 
Thank you I will give it a try.

Ken Tucker said:
Hi,

You need to create an inherited datagrid for that. Here is the code
for one that will raise an event to let you the user is about to delete a
row. Also prevents the user from resizing a row or column.

Public Class ConfirmDeleteDataGrid

Inherits DataGrid

Public Event DeletedRow(ByVal sender As Object, ByVal e As EventArgs)

Private Const WM_KEYDOWN = &H100



Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean

Dim keyCode As Keys = CType((msg.WParam.ToInt32 And Keys.KeyCode), Keys)

If msg.Msg = WM_KEYDOWN And keyCode = Keys.Delete Then

If MessageBox.Show("Delete This Row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean

Dim pt As Point

Dim hti As DataGrid.HitTestInfo

pt = Me.PointToClient(Cursor.Position)

hti = Me.HitTest(pt)

If keyData = Keys.Delete Then

If hti.Type = Me.HitTestType.RowHeader Then

If MessageBox.Show("Delete this row?", "Confirm Delete", _

MessageBoxButtons.YesNo) = DialogResult.No Then

Return True

Else

RaiseEvent DeletedRow(Me, New EventArgs)

End If

End If

End If

Return MyBase.ProcessDialogKey(keyData)

End Function



Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)

End Sub

Public Sub New()

Trace.WriteLine(Me.VertScrollBar.Visible.ToString)

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)

Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X, e.Y))

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)

End Sub


End Class



Ken

-------------------------------


I have a question about these events that I hope someone could help me with.

I have a DataGrid bound to a DataSet. The grid's datasource is a DataView
and deletes are allowed.

I have added a Event Handler for OnRowDeleting.

What I want to do, is to only allow deletes to occur for newly added records
(before DataSet.Update is run). I want to cancel deletes for existing
records.

The code for OnRowDeleting is:

Private Sub OnRowDeleting(ByVal sender As Object, ByVal e As
DataRowChangeEventArgs)
'=============================================================
' This event fires when a row is being deleted
|
'=============================================================
Try
If e.Row.RowState <> DataRowState.Added Then
Throw (New DeleteAgencyException("Cannot Delete an Existing
Row"))
End If
Catch ex As DeleteAgencyException
MessageBox.Show(ex.Message, "Delete Agency")
e.Row.RejectChanges()
End Try
End Sub

What happens is my Exception is captured and the message displayed, but the
row is still deleted. The RejectChanges doesn't seem to actually reject the
changes. I tried CancelEdit and got the same results. How can I cancel the
changes?

Thanks
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top