I have a database and after I delete a row I cannot update database, I'm getting error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll update requires a valid updatecommand when passed datarow collection with deleted rows
... indicating line: sqliteDataAdapter.Update(dataTable);
Here's my code:
private void SaveChanges(bool keepControlsDisabled)
{
disableControls();
Validate();
programs_dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
programs_dgv.EndEdit();
programs_dgv.DataSource = null;
programs_dgv.DataSource = bindingSource;
programs_dgv.Update();
sqliteDataAdapter = new SQLiteDataAdapter();
sqliteDataAdapter.Update(dataTable);
dataTable.AcceptChanges();
if (keepControlsDisabled == false)
{
enableControls();
}
}
Here's the delete code:
private void contextMenuStrip_tsmi_Click(object sender, EventArgs e)
{
Int32 rowToDelete = programs_dgv.Rows.GetFirstRow(DataGridViewElementStates.Selected);
programs_dgv.Rows.RemoveAt(programs_dgv.CurrentCell.RowIndex);
programs_dgv.ClearSelection();
}
I've read the question at Saving changes from a DataGridView back to an SQL database? but that didn't help.
Is there a universal save function to pass records from datagridview to database?
Am on the right way? Do I have to deal with database and display results on datagridview? Or firstly datagridview must be handled? I'm so confused with that. Which one is right for performance?