editable datagridview c#

ankit goel 746 Reputation points
2023-05-24T14:05:13.5433333+00:00

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 row and first column 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);

    }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,871 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,386 Reputation points
    2023-05-26T11:25:55.06+00:00

    Okay, the following should work for you when you use the DataGridView.DataSource to populate the DataGridView. I tested this out and works. Note the Timer, without the timer focus is lost when in edit more. Also note as coded the code only works the first time and only if the DataSource is not null.

    public class PayneDataGridView : DataGridView
    {
        private bool _firstTime = true;
        protected override void Select(bool directed, bool forward)
        {
            if (DataSource != null)
            {
                if (_firstTime)
                {
                    CurrentCell = Rows[0].Cells[0];
                    Timer timer = new Timer();
                    timer.Interval = 20;
                    timer.Tick += (ts, te) => {
                        timer.Stop();
                        BeginEdit(false);
                    };
                    timer.Start();
    
                    _firstTime = false;
                }
            }
            else
            {
                base.Select(directed, forward);
            }
        }
    }
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2023-05-24T16:47:37.05+00:00

    Here is a start outside of the control, subscribe to Enter event e.g

    customersDataGridView.Enter += CustomersDataGridViewOnEnter;
    
    

    The event

    private void CustomersDataGridViewOnEnter(object sender, EventArgs e)
    {
        customersDataGridView.Rows[0].Cells[0].Selected = true;
        customersDataGridView.BeginEdit(false);
        customersDataGridView.Enter -= CustomersDataGridViewOnEnter;
    }
    

    If that works than integrate into your custom DataGridView or use as is.

    And this post embodies what I presented.