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.
Unfortunately, activation email could not send to your email. Please try again.

Deletion in DataGrid

Thread ID:

Created:

Updated:

Platform:

Replies:

186 Apr 23,2002 12:12 AM Apr 29,2002 02:02 AM Windows Forms 3
loading
Tags: FAQ
loh
Asked On April 23, 2002 12:12 AM

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

loh
Replied On 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.

Clay Burch [Syncfusion]
Replied On 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

CONFIRMATION

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.

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.

;