4
votes

Hi
I have a DataGridView which is bound to an XML source.

  1. I have a problem in editing cells. The cell on click becomes selected and when it is edited, by default we overwrite it. My requirement says it should be ready for editing and not selected when clicked.
  2. I want to generate a row dynamically whenever the 'tab' key is pressed.

How can I achieve this?

2

2 Answers

2
votes

With respect to question 1)

You can try this:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            this.dataGridView1.CellEnter += new DataGridViewCellEventHandler(myDataGrid_CellEnter);
        }
        void myDataGrid_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if ((this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewTextBoxColumn) ||
                (this.dataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn))
            {
                this.dataGridView1.BeginEdit(false);
            }
        }
4
votes

If I understand you correctly you want the cell to enter edit mode as soon as it is clicked. This can be achieved by setting the EditMode property of the DataGridView to EditOnEnter. This leaves the text in the editing control selected however, so if you don't want that you could use:

dataGridView1_CurrentCellChanged(object sender, EventArgs e)
{
        dataGridView1.BeginEdit(false);
}

Can you explain what you mean by adding row dynamically?