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.