Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / October 2004

Tip: Looking for answers? Try searching our database.

Winforms datagrid keydown problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Srikanth - 28 Oct 2004 08:45 GMT
Hi,
    The Keydown event on a Winforms Datagrid only fires some times and I
want it to fire at all times. I am trying to catch the Keydown event and
check whether the key pressed is 'Delete' on a datagrid row.

Am I missing something?

Any help is appreciated. Thanks in advance.
Srikanth.
"Jeffrey Tan[MSFT]" - 29 Oct 2004 03:22 GMT
Hi Srikanth,

Based on my understanding, you want to intercept the "Delete" key down
event in DataGrid.

Yes, most of the time, DataGrid's KeyDown event will work well, but if we
press key in DataGridTextBoxColumn, this event handler will not fire when
pressing. This is because, in edit mode, there is actually a TextBox in
that column, and this textbox has the focus, so only the TextBox's keydown
event will fire.

For your requirement, we have 2 options:
1. If you just want to be notified when user press "Delete" key, we may
hook both DataGridTextBoxColumn and the DataGrid's keydown event. We should
explicitly add a DataGridTextBoxColumn into the datagrid, then get the
reference of TextBox in that column and hook its KeyDown event, like this:
private void Form1_Load(object sender, System.EventArgs e)
{
    DataGridTextBoxColumn dgtbc=new DataGridTextBoxColumn();
    dgtbc.MappingName="binding column name";
    dgtbc.TextBox.KeyDown+=new KeyEventHandler(TextBox_KeyDown);
    DataGridTableStyle dgts=new DataGridTableStyle();
    dgts.GridColumnStyles.Add(dgtbc);
    dgts.MappingName="datasource's mapping name";

    this.dataGrid1.TableStyles.Clear();
    this.dataGrid1.TableStyles.Add(dgts);
}

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{   
    if(e.KeyCode==Keys.Delete)
    {
        MessageBox.Show("TextBox_KeyDown");
    }
}

private void InitializeComponent()
{
    this.dataGrid1.KeyDown += new
System.Windows.Forms.KeyEventHandler(this.dataGrid1_KeyDown);
}

private void dataGrid1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
    if(e.KeyCode==Keys.Delete)
    {
        MessageBox.Show("dataGrid1_KeyDown");
    }
}

2. If you not only want to be notified, but also want to do something to
this keyevent(such as disable the delete keydown operation), we may inherit
from normal DataGrid class, and override its ProcessCmdKey method, and
intercept the "delete" keydown return "true" to indicate this event has
been handled(so the following operation won't take place, that is the
delete function is disabled!), sample code like this:

const int WM_KEYDOWN=0x100;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if(msg.Msg==WM_KEYDOWN)
    {
        if(keyData==Keys.Delete)
        {
            MessageBox.Show("Delete");
            return true;   //disable the following delete function
        }
    }
    return base.ProcessCmdKey (ref msg, keyData);
}
This both works well on my side.

For more information about FAQ about Winform datagrid, please refer to:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp
You will find many useful information here.
===================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.