I have set
EditMode = DataGridViewEditMode.EditProgrammatically;
on my DataGridView because I do not want repeated single clicks to start edit mode. (This happens with all other EditMode settings).
Now I have overridden OnKeyDown and called BeginEdit to start editing the cells when the user is typing.
protected override void OnKeyDown(KeyEventArgs e)
{
BeginEdit(true);
base.OnKeyDown(e);
}
The problem is that the event handling seems to swallow the first keypress or maybe BeginEdit is called too late.
Here is some code that can be pasted into a new VS C# project that demonstrates the problem.
(Just replace the generated Form1 definition when pasting.)
public class MyDataGridView : DataGridView
{
public MyDataGridView()
{
this.EditMode = DataGridViewEditMode.EditProgrammatically;
}
protected override void OnKeyDown(KeyEventArgs e)
{
BeginEdit(true);
base.OnKeyDown(e);
}
}
public partial class Form1 : Form
{
private System.Windows.Forms.DataGridView dataGridView1;
public Form1()
{
this.dataGridView1 = new MyDataGridView();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.Controls.Add(this.dataGridView1);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
this.dataGridView1.Columns.Add("", "foo");
this.dataGridView1.Columns.Add("", "bar");
this.dataGridView1.Rows.Add(3);
}
}
You can try overriding the OnKeyPress event instead and send the key that was pressed: