Put up a confirmation question when the user tries to delete a row in the datagrid

S

Suzanne

I'd like to know how can I put up a confirmation question when the
user tries to delete a row in the datagrid by clicking on the row
header and pressing the Delete key?

I have found this code on a windows forms FAQ site :

Public Class DataGrid_Custom
Inherits DataGrid

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
End If
End If
Return MyBase.PreProcessMessage(msg)
End Function
End Class

but it does not work consistently - ie when I first click into the
rowheadercell of the row I want to delete, the row imediatley goes in
to edit mode (with the pencil icon) and the row deletes without the
message. If I click out of the row and then go back in ie when the
arrow icon shows in the row header cell the code works properly and I
get the confirmation question.
Is there someway that I can make the datagrid only go into edit mode
when I click into a textbox within in it and not when I click on the
header cells?

I want to be able to do this using key up/down/press events of the
datagrid and not use e.Action.Delete in
System.Data.DataRowChangeEventArgs of the datatable as I don't want
to call acceptchanges on my datatable (since this will create problems
further for me further down the line)

Any ideas would be gratefully recieved
Thanks
Suzanne
 
J

Jun Wan

Would you please debug the code to check if the function PreProcessMessage
is called when you select the row at the first time?

Regards,
Justin Wan
Microsoft Partner Online Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jun Wan

No function is called? It is really strange. Would you please send me an
entire sample project to have a look?

Regards and thanks,
Justin Wan
Microsoft Partner Online Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Suzanne James

Hello Justin

(is that correct or do you prefer Jun?)

As you requested, here is an example project that demonstrates the
problem I'm having - i.e that if I single click in the header cell of
the data grid ( ccDataGridExample) I don't get the confirmation
question. But if I double click in the header
cell then I do get the confirmation question. (& I think the way that
most users would try to delete a data row is by just clicking once on
the data row that they intend to delete and then pressing delete key!)

Excuse the very basic code (I'd never do this real world) but it does
demonstrate the problem. Please let me know if there is any other
information you require.

Any help / advice you could provide on this would be most appreciated

Many thanks

Suzanne




Public Class ExampleDataGrid

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call


End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents CcDataGrid1 As ccDataGridExample

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.CcDataGrid1 = New
WindowsApplication1.ExampleDataGrid.ccDataGridExample

CType(Me.CcDataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'CcDataGrid1

'

Me.CcDataGrid1.DataMember = ""

Me.CcDataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText

Me.CcDataGrid1.Location = New System.Drawing.Point(16, 24)

Me.CcDataGrid1.Name = "CcDataGrid1"

Me.CcDataGrid1.Size = New System.Drawing.Size(424, 160)

Me.CcDataGrid1.TabIndex = 1

'

'ExampleDataGrid

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(464, 266)

Me.Controls.Add(Me.CcDataGrid1)

Me.Name = "ExampleDataGrid"

Me.Text = "Form1"

CType(Me.CcDataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

End Sub

#End Region

Public Class ccDataGridExample

Inherits System.Windows.Forms.DataGrid

Private Const WM_KEYDOWN = &H100

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

Dim dialog As New DialogResult

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) = dialog.No Then

Return True

End If

End If

Return MyBase.PreProcessMessage(msg)

End Function

End Class

Dim dt_datatable As New DataTable

Private Sub addColumns()

dt_datatable.Columns.Add("Column1")

dt_datatable.Columns.Add("Column2")

dt_datatable.Columns.Add("Column3")

dt_datatable.Columns.Add("Column4")

End Sub

Private Sub addRows()

Dim int_counter As Integer

For int_counter = 0 To 5

Dim dr_NewRow As DataRow = dt_datatable.NewRow()

dr_NewRow("Column1") = "Value1"

dr_NewRow("Column2") = "Value2"

dr_NewRow("Column3") = "Value3"

dr_NewRow("Column4") = "Value4"

dt_datatable.Rows.Add(dr_NewRow)

Next

End Sub

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)

addColumns()

addRows()

CcDataGrid1.DataSource = dt_datatable

End Sub

End Class
 
J

Jun Wan

Hello Suzanne,

I have sent email to you at (e-mail address removed). You can directly
contact me by directly replying me.

Regards,
Justin Wan
Microsoft Partner Online Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top