views:

323

answers:

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!

A: 
dataGridView.EndEdit();
bSource.EndEdit();
dAdapter.Update(dTable);

Works!

After a lengthy search I finally found a clue:

Kudos -> .NET WinForms End Current Edit

Hoax
A: 

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.

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