I have created a custom DataGridview for my project. I am trying to find a way by which when my datagridview got focused, it should go into editable mode. what i mean is its first cell of first cell should go into editable mode. what modifications do I have to make here to achieve this.
public partial class CustomControl1: DataGridView
{
public CustomControl1()
{
this.KeyDown += new KeyEventHandler(CustomDataGridView_KeyDown);
InitializeComponent();
}
protected override bool ProcessKeyPreview(ref Message m)
{
KeyEventArgs args1 = new KeyEventArgs(((Keys)((int)m.WParam)) | Control.ModifierKeys);
switch (args1.KeyCode)
{
case Keys.Left:
case Keys.Right:
case Keys.Up:
case Keys.Down:
return false;
}
return base.ProcessKeyPreview(ref m);
}
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
this.EndEdit();
int row = this.CurrentCell.RowIndex;
int col = this.CurrentCell.ColumnIndex;
if (col == 0 && string.IsNullOrEmpty(this.CurrentCell.Value?.ToString()))
{
this.SelectNextControl(this, true, true, true, true);
return true;
}
else if (col == this.ColumnCount - 1) // CHECK IF I AM AT LAST COLUMN
{
if (row == this.RowCount - 1)// CHECK IF I AM AT LAST ROW
{
this.Rows.Add();
this.CurrentCell = this[0, row + 1];
this.BeginEdit(true);
return base.ProcessDialogKey(keyData);
}
else
{
this.CurrentCell = this[0, row + 1];
}
}
else // not at last column
{
this.CurrentCell = this[col + 1, row]; // change column
this.BeginEdit(true);
}
return true;
}
return base.ProcessDialogKey(keyData);
}