Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts,

in an application I am developing there is a DataGridView. For reasons out of the scope of this question user shall not be allowed to edit the grid's content directly.

Instead, on beginning cell edit, the operation is immediately cancelled. A textbox is being displayed right where the original cell would have been.
MyDataGridView.cs:
C#
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
{
    e.Cancel = true;

    MyTextBox textBox = new MyTextBox(
        e.ColumnIndex, e.RowIndex,
        this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()
    );

    System.Drawing.Rectangle cellRectangle = this.GetCellDisplayRectangle(
        e.ColumnIndex, e.RowIndex,
        false
    );

    textBox.Location = cellRectangle.Location;
    textBox.Size = cellRectangle.Size;
    textBox.Finished += new EventHandler(textBox_Finished);
    this.Controls.Add(textBox);
    textBox.Focus();
}
User can then edit int the special text box.
MyTextBox.cs:
C#
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
    base.OnKeyDown(e);

    if (e.KeyCode == System.Windows.Forms.Keys.Escape)
    {
        this.Text = _initialText;

        if (Finished != null)
            Finished(this, new EventArgs());
    }
    else if (e.KeyCode == System.Windows.Forms.Keys.Enter)
    {
        if (Finished != null)
            Finished(this, new EventArgs());
    }
}
The Finished event handler then transfers the text box value to the grid and destroys the text box.

My problem at the moment is that above OnKeyDown event doesn't fire on Enter nor on zero nor on arrow keys. Other keys seem to work as expected.

How can I get all keys to work?
Posted
Updated 24-Nov-11 3:16am
v2
Comments
LanFanNinja 24-Nov-11 8:29am    
Have you put break points at these locations to verify that they are indeed not firing? I really see no reason this is not working for you.
I tried to replicate your solution checking for Keys.D0, Keys.Enter, Keys.Left and they all worked as should for me.
lukeer 24-Nov-11 9:15am    
Yes, I put a breakpoint in the OnKeyDown event handler.
Enter makes the underlying DataGridView change the selected cell without triggering OnKeyDown in MyTextBox.
Zero (D0 as well as NumPad0) is kind of strange. It doesn't trigger MyTextBox.OnKeyDown event handler as well. But it seems to toggle MyTextBox's text between two values: the initial text and another that user entered.

Arrow keys work just normal. I'm sorry for that misinformation.

Handle KeyDown as well - KeyPress is only for text keys, not command keys.
 
Share this answer
 
Comments
lukeer 24-Nov-11 8:48am    
As you can see in the second code example, I did handle OnKeyDown.
I've solved it by placing the DataGridView on an additional Panel, and then placing the TextBox on that Panel as well instead of placing it on the DataGridView.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900