在WinForms中,可以使用以下方法更新DataGridView的數據:
dataGridView.DataSource = dataSource; // 將數據源綁定到DataGridView
// 修改數據源
dataSource[index].Property = newValue;
// 刷新DataGridView顯示的數據
dataGridView.Refresh();
private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
// 修改和更新數據
foreach (DataGridViewRow row in dataGridView.Rows)
{
// 獲取數據并修改
var data = (DataRowView)row.DataBoundItem;
data["Property"] = newValue;
}
}
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = dataSource; // 設置數據源
dataGridView.DataSource = bindingSource; // 綁定BindingSource到DataGridView
// 修改BindingSource的數據
bindingSource[index].Property = newValue;
// 更新DataGridView的顯示
bindingSource.ResetBindings(false);