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";
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";
您需要在 datagridview 上调用刷新
datagridview1.Refresh();
如果您处于编辑模式,那么您应该使用 EndEdit
datagridview1.EndEdit();
datagridview1.Refresh();
DataSet
你需要在我的代码下面写回
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]);
}
}
我认为您需要手动执行此操作。将数据绑定到gridview。更新网格视图。选择已更新的单元格,将数据保存回数据并从数据源中提取数据并刷新网格视图。
我有同样的问题,能够通过在绑定源上调用 EndEdit() 来解决它。
例如
bs.EndEdit();
int x1 = data.Update(dt);
然后更新返回更新的行数。在添加 EndEdit 之前,它始终为零