I'm just starting to learn C# and have been playing around with a simple LocalDB and a datagridview showing my data. I have two text boxes and a button for add record, one for save changes to record.
I am struggling to add a row to my datagridview and also struggling to save my changes to the database (I can get them to show on the grid after I filled out my text boxes, but nothing saves to the SQL database.
My dataset is called databaseDataSet, my binding source is called contactsBindingSource and contactsTableAdapter is my table adapter.
What I have tried:
For a new row I have tried:
private void btnNew_Click_(object sender, EventArgs e)
{
dataGridView1.Rows.Add();
}
However, This gives me the following error "Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound." I see now I need to add the row to the table in the data source but how do I do this?
For saving my changes I have tired:
private void btnSave_Click(object sender, EventArgs e)
{
contactsTableAdapter.Update(databaseDataSet.contacts);
contactsBindingSource.EndEdit();
databaseDataSet.AcceptChanges();
This updates the information in my grid to that of the textbox, but doesn't save it to the SQL database? If I close the program it all resets. Can anyone explain what I should be using?
I also tried:
this.Validate();
this.contactsBindingSource.EndEdit();
this.contactsTableAdapter.Update(this.databaseDataSet);
This appears to do nothing
Any help appreciated.
Dan