0
votes

After I change the DataGridView cell's value programmatically, the source cannot be updated. What can I do?

datagridview1.DataSource = bindingsource1;
datagridview1.Rows[0].Cells[0].Value = "1";
4

4 Answers

0
votes

You need to call refresh on datagridview

datagridview1.Refresh();

If you are in edit mode then you should use EndEdit

datagridview1.EndEdit();
datagridview1.Refresh();
0
votes

I think you need to do this manually. Bind the data to the gridview. Update the gridview. Select the cell that was updated, save the data back to the data and pull the data from the datasource and refresh the gridview.

0
votes

You need write back the DataSet, here below my codes

private void button2_Click(object sender, EventArgs e)
{
    this.Validate();

    try
    {
        dgvArticles.CurrentRow.Cells[1].Value = txtSubject.Text;
        dgvArticles.CurrentRow.Cells[2].Value = rtbBodyContent.Text;
        dgvArticles.CurrentRow.Cells[3].Value = pbPrimaryPicture.Image;
        dgvArticles.CurrentRow.Cells[4].Value = pbSecondaryPicture.Image;
        dgvArticles.CurrentRow.Cells[5].Value = pbThirdPicture.Image;
    }
    catch
    {
        MessageBox.Show(e.ToString());
    }

    AccessingNetFamerDatabase anfdArticles = new AccessingNetFamerDatabase();
    if (_dsArticles!= null)
    {
        SqlCommandBuilder _sqlCBArticles = new SqlCommandBuilder(AccessingNetFamerDatabase._sqlDataAdapter);
        AccessingNetFamerDatabase._sqlDataAdapter.Update(_dsArticles.Tables[0]);
    }
}
0
votes

I has the same issue, was able to solve it by calling EndEdit() on the binding source.

eg

bs.EndEdit();

int x1 = data.Update(dt);

Update then returned the number of rows updated. Before the EndEdit was added it was constantly zero