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

Deletion in DataGrid

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?

3 Replies

CB Clay Burch Syncfusion Team April 23, 2002 05:54 AM

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);
	}
}


LO loh April 28, 2002 06:55 PM

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.


CB Clay Burch Syncfusion Team April 29, 2002 02:02 AM

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

Loader.
Live Chat Icon For mobile
Up arrow icon