2

I have a nice litte DataGridView, which gets loaded/populated by a button and a SQLDataAdapter and the corresponding saveButton.

private void loadButton_Click(object sender, EventArgs e)
{

    String connString = conStringComboBox.Text;
    String query = queryStringComboBox.Text;

    dAdapter = new SqlDataAdapter(query, connString);
    SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

    dTable = new DataTable();

    dAdapter.Fill(dTable);

    bSource = new BindingSource();

    bSource.DataSource = dTable;

    dataGridView.DataSource = bSource;

}


private void saveButton_Click(object sender, EventArgs e)
{
    dAdapter.Update(dTable);
}

Whenever I edit something in the dataGridView and click save everything is just peachy. The value in the correct cell on the the SQL-Server gets updated.

BUT I implemented a little editor for better overview. Value of Cell X is changed there and it gets back to the DataGridview:

dataGridView.SelectedCells[0].Value = exampleString;

DataGridView Updates accordingly, I click save - Update gets executed without a hitch, but the new value obviously isn't committed to the SQL Server, because as soon as I load the same table it is displaying the old value again.

Any ideas where what I forgot here? Every Input is appreciated!

2 Answers 2

2
dataGridView.EndEdit();
bSource.EndEdit();
dAdapter.Update(dTable);

Works!

After a lengthy search I finally found a clue:

Kudos -> .NET WinForms End Current Edit

Sign up to request clarification or add additional context in comments.

Comments

0

Since you are bound to a datatable, the rows have a rowstate (original, modified). When using the in build edit function in the grid this sets the rows state to modified, maybe using your method to update the cell programatically is not setting the underlying sources datarow property to modified, or worse not even changing it's value.

1 Comment

I wasn't exiting the editState properly so you were on the money there, thx for the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.