Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Preventing default behaviour in a datagridview
Message
From
20/01/2008 15:57:19
 
 
To
20/01/2008 14:41:03
John Thomason
King Richard Veterinary Centre
Leicester, United Kingdom
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Database:
MS SQL Server
Miscellaneous
Thread ID:
01283445
Message ID:
01283458
Views:
17
John,

First, you have to be using your own Grid, sub-classed from the DataGridView.

Next, in your sub-class, you need to override the ProcessCmdKey() method. You'll want something like this (this code uses a switch-case, to add code for trapping other keys):
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	const int WM_KEYDOWN = 0x100;
	const int WM_SYSKEYDOWN = 0x104;

	if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
	{
		switch (keyData)
		{
			case Keys.Enter:
				// do whatever you need to do
				return true;
		}
	}

	return base.ProcessCmdKey(ref msg, keyData);
}
~~Bonnie


>When you press Enter on a record in a datagridview, focus moves to the next record. Can anyone tell me how to prevent this behaviour ie remain on the same record when Enter is pressed. I want to choose a record and hide the datagridview using the keyboard, not the mouse.
>
>Any help appreciated.
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform