1

I'm writing a simple WinForms program in C# with Visual Studio 2015, and trying to get File/New to clear the data grid (which it's doing) and regain focus for further data entry (which it's not exactly doing).

I'm using a DataSource:

BindingList<Record> Data = new BindingList<Record>();
...
Data.AllowNew = true;
...
dataGridView1.DataSource = Data;
dataGridView1.AllowUserToAddRows = true;
dataGridView1.AllowUserToDeleteRows = true;

and File/New does this:

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (Unsaved())
    {
        var r = MessageBox.Show("Proceed without saving?", "Unsaved data", MessageBoxButtons.YesNo);
        if (r != DialogResult.Yes)
            return;
    }
    Data.Clear();
    dataGridView1.Refresh();
    dataGridView1.Focus();
}

That resets the grid to a single blank row, but it does not regain the focus. What am I missing?

Select is also supposed to work, but doesn't. A WinForms project can't be reduced to a single file test case as far as I know, but the actual code at https://github.com/russellw/adder/blob/master/Form1.cs is pretty close to a minimal WinForms project; steps to reproduce are just run the project under the debugger and select File/New or ctrl-N shortcut, observe that the focus does not end up on the grid view.

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    Works for me. `dataGridView1.Select();` is probably the preferred method. What control gets the focus? – LarsTech Oct 20 '16 at 22:30
  • 1
    Hm, it should and it does here. – TaW Oct 20 '16 at 22:57
  • No control appears to have the focus, but pressing tab or any cursor key makes focus go back to the grid. Behavior is the same whether using Focus() or Select(). Actual code is at https://github.com/russellw/adder/blob/master/Form1.cs – rwallace Oct 20 '16 at 23:05
  • I agree with LarsTech. It worked for me. Your posted code for the newToolStripMenuItem_Click always returned focus to the dataGridView. If the dataGridView had focus when I clicked the new menu item then the dataGridView cell[0.0] was not selected... but pressing the "Enter/Tab/Arrow" key gave cell[0,0] focus. If some other component had focus upon entering event newToolStripMenuItem_Click the dataGridView cell[0,0] got focus. Again your code seems to work. – JohnG Oct 23 '16 at 07:43

3 Answers3

2

dataGridView's Select() and Focus() methods are not what you are looking for I guess. Even control got focus, you need to tell control what is the selected cell for further data entry. FirstDisplayedCell property can be the solution.

You can try this,

dataGridView1.CurrentCell = dataGridView1.FirstDisplayedCell;
dataGridView1.CurrentCell.Selected = true;

The result will be (selected with blue),

enter image description here

If you add this (you can set it again somewhere else to the default),

dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;

The result will be (it is selected but not with blue highlight),

enter image description here

And the result as gif,

enter image description here

Hope helps,

Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
1

Got some help from here: DataGridView - Focus a specific cell.

Hope this is what you are looking for.

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
  if (Unsaved())
  {
    DialogResult r = MessageBox.Show("Proceed without saving?", "Unsaved data", MessageBoxButtons.YesNo);
    if (r != DialogResult.Yes)
     return;
  }
  Data.Clear();
  dataGridView1.Rows.Clear();
  //dataGridView1.Refresh();
  //dataGridView1.Focus();
  dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
  dataGridView1.Rows[0].Cells[0].Selected = true;
  dataGridView1.BeginEdit(true);
}
Community
  • 1
  • 1
JohnG
  • 9,259
  • 2
  • 20
  • 29
0

You can try by

dataGridView1.Select();

or

ActiveControl = dataGridView1;
Mostafiz
  • 7,243
  • 3
  • 28
  • 42