We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Deletion in DataGrid

Thread ID:





186 Apr 23,2002 04:12 AM UTC Apr 29,2002 06:02 AM UTC WinForms 3
Tags: FAQ
Asked On April 23, 2002 04:12 AM UTC

Is there a event in the Windows Forms DataGrid similar to the BeforeDelete event in VB6 DataGrid? I would like to display a message box to confirm the deletion. How can I achieve that?

Clay Burch [Syncfusion]
Replied On April 23, 2002 09:54 AM UTC

One was you can catch the Delete key is to override the DataGrid's PreProcessMessage. Here is a code snippet.

public class MyDataGrid : DataGrid
	const int WM_KEYDOWN = 0x100;

	public override bool PreProcessMessage(	ref Message msg )
		Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
		if(msg.Msg == WM_KEYDOWN && keyCode == Keys.Delete)
			if(MessageBox.Show("Delete this row?", 
				"", MessageBoxButtons.YesNo) == DialogResult.No)
			return true;
		return base.PreProcessMessage(ref msg);

Replied On April 28, 2002 10:55 PM UTC

Thx Clay for this very useful tip. I notice that this message will still be fired when the AllowDelete property of the dataview that is bound to the grid is set to false. Though answering Yes or No has no effect. How can I add in a condition that if AllowDelete = false, no delete confirm message will be display? Thanks in advance.

Clay Burch [Syncfusion]
Replied On April 29, 2002 06:02 AM UTC

As long as you are sure the DataSource is a DataView, you could directly test for AllowDelete in the override. if(msg.Msg == WM_KEYDOWN && keyCode == Keys.Delete && ((DataView) this.DataSource).AllowDelete) { if(MessageBox.Show("Delete this row?", "", MessageBoxButtons.YesNo) == DialogResult.No) return true; } Regards, Clay


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon