Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagridview KeyDown Event
 

Datagridview KeyDown Event

I have a datagridview bound to a datatable. I want to have the Enter key move across the row and not down the column. I can catch the Enter key with a KeyDown event to use a SendKeys command (not very elegent), but when the cell is in the edit mode, the KeyDown event is not fired when the Enter Key is pressed. The focus moves down the column. Any ideas?
ScottL  Friday, November 04, 2005 9:06 PM

Check out the following post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=157055&SiteID=1

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�/span>

Mark Rideout  Monday, December 05, 2005 6:19 AM
Hi Scott,

I also had thesame problem and use this as a solution: it is not yet perfect though, but perhaps  you find it usefull:



public class DataGridViewEnter : DataGridView
    {
        const int WM_KEYDOWN = 0x100;

        protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
        {
            if (msg.Msg == WM_KEYDOWN && keyCode == Keys.Enter)
            {
                msg.WParam = (IntPtr)Keys.Tab;
                keyData = Keys.Tab;
             }
            return base.ProcessCmdKey(ref msg, keyData);
        }


    }
 

 

Paul Diterwich  Thursday, November 17, 2005 2:49 PM
Hi there.

That is the only fix ive found but it doesnt solve the problem. That doesnt work when the cell is being edited.

There are some threads with this matter  but i havent found nothing that solves this problem. If theres some solution please let me know.

Thank you very much
MacKraken  Sunday, December 04, 2005 3:28 PM

Check out the following post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=157055&SiteID=1

 

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is�/span>

Mark Rideout  Monday, December 05, 2005 6:19 AM

This modified version also adds a new row automatically and hops back to the first column after an enter given in the last column a row higher:



public class DataGridViewEnter : DataGridView
 {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                return this.ProcessRightKey(keyData);
            }
            return base.ProcessDialogKey(keyData);
        }

       public new bool ProcessRightKey(Keys keyData)
        {
            Keys key = (keyData & Keys.KeyCode);
            if (key == Keys.Enter)
            {
                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex == (base.RowCount - 1)))
                {
                    ((BindingSource)base.DataSource).AddNew();
                    base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
                    return true;
                }

                if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex + 1 != base.NewRowIndex))
                {
                    base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
                    return true;
                }
                return base.ProcessRightKey(keyData);
            }
            return base.ProcessRightKey(keyData);
        }

        protected override bool ProcessDataGridViewKey(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                return this.ProcessRightKey(e.KeyData);
            }
            return base.ProcessDataGridViewKey(e);
        }

 



Hope you guys find it usefull.

Cheers,

Paul

Paul Diterwich  Wednesday, December 07, 2005 1:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Allowing the user to choose the system dsn
• Passing values between windows forms
• Get structure of DataGridView from the DataSource at runtime
• to apply Docking Style to the grid view using c# in ado.net
• Can a DataViewGrid have a RadioButton-like setup?
• Prevent edit DataGridViewCheckBoxCell
• Syntax error in INSERT INTO statement in vb2008 express and 2007accdb
• GridView edit button, Catch button push
• Datagrid problem
• How to use the DataNavigateUrlFormat String or maybe im using the wrong property to link to a file path