2

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);

    }
ankit goel
  • 43
  • 6
  • 1
    Override `OnEnter()` and add `BeginEdit(true);`, if you just want to start editing a Cell when the Control is entered using TAB. If you also need to handle Mouse interaction, then it depends on the `SelectionMode`: if it's, e.g., `CellSelect`, you just need `CurrentCell = SelectedCells[0]; BeginEdit(true);`, otherwise (e.g., `FullRowSelect`), you need to determine the Cell clicked using the `HitTest()` method, then make that Cell the current and `BeginEdit()` again (so you probably just want to use this second method) – Jimi May 24 '23 at 14:30
  • To note that you can modify the Edit Mode of Cells, which has different switches – Jimi May 24 '23 at 14:32

1 Answers1

0

thanks to @jimi ,

the code would be

protected override void OnEnter(EventArgs e)
    {
        this.CurrentCell = this.Rows[0].Cells[0];
        this.BeginEdit(true);
       base.OnEnter(e);
    }
ankit goel
  • 43
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '23 at 07:36