Hello,
I know that this question was here quite a few times but I was wondering if
anyone atually found a solution/work around for it.
I'm trying to prevent users from deleting certain rows in datagrid. This
rows are specified in rownumber property. To do this I have implemented
custom datagrid that looks like this:
Class DGCustom
Inherits DataGrid
Private Const WM_KEYDOWN As Integer = &H100
Friend intRowNumber As Integer = 0
Friend Property rownumber() As Integer
Get
Return intRowNumber
End Get
Set(ByVal Value As Integer)
intRowNumber = Value
End Set
End Property
Public Overrides Function PreProcessMessage(ByRef msg As
System.Windows.Forms.Message) As Boolean
Dim keycode As Keys
keycode = 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
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 Me.CurrentCell.RowNumber < rownumber Then
MessageBox.Show("This is a system row and it cannot be changed or deleted")
Return True
Exit Function
End If
If keydata = Keys.Delete And hti.Type = Me.HitTestType.RowHeader Then
If MessageBox.Show("Delete this row?", "Confirm Delete",
MessageBoxButtons.YesNo) = DialogResult.No Then
Return True
End If
End If
End Function
End Class
This works as long as they don't select more than one row. If two or more
rows are selected, including the one that can be deleted, the protected rows
get deleted as well.
I have also tried something like
Private Sub dtDS_RowDeleting(ByVal sender As Object, ByVal e As
System.Data.DataRowChangeEventArgs) Handles dtDS.RowDeleting
If dgDS.CurrentRowIndex < dgDS.rownumber Then
e.Row.RejectChanges()
End If
End Sub
but this rejectChanges does not stop the deletion.
Thanks in advance ...