If all you are doing are simple CRUD operations than consider a TableAdapter which write all the code for you but be forewarned going farther than simple CRUD operations you need to fully understand how to use the TableAdapter. They have no problem with a 1,000 records.
Sample code which I modified from what Microsoft wrote.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Closing += OnClosing;
personDataGridView.DataError += DataGridViewDataError;
}
private void DataGridViewDataError(object sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
MessageBox.Show("Invalid input");
}
private void OnClosing(object sender, CancelEventArgs e)
{
if (dataSet1.Tables[0].HasChanges())
{
if (Question("Save before exiting"))
{
SaveChanges();
}
else
{
e.Cancel = false;
}
}
}
private void personBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
SaveChanges();
}
private void SaveChanges()
{
Validate();
personBindingSource.EndEdit();
tableAdapterManager.UpdateAll(dataSet1);
}
private void Form1_Load(object sender, EventArgs e)
{
personTableAdapter.Fill(dataSet1.Person);
}
}
If using .NET Framework a BindingNavigator is placed on the form to make standard operations easy while with .NET Core the BindingNavigator is missing. Add the follow class to get the BindingNavigator in a .NET Core project.
public class CoreBindingNavigator : BindingNavigator
{
public CoreBindingNavigator()
{
AddStandardItems();
}
}
And the Question dialog
public static class Dialogs
{
public static bool Question(string text) =>
(MessageBox.Show(
text,
Application.ProductName,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) == DialogResult.Yes);
}