问题描述
我有一个很棒的DataGridView,它被一个按钮和一个SQLDataAdapter加载/填充,并且相应的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);
}
每当我在dataGridView中编辑某些东西,然后点击保存,一切都只是peachy。 SQL Server上正确的单元格中的值将被更新。
但是我实现了一个小编辑器来更好地概述。单元格X的值在那里被改变,并返回到DataGridview:
dataGridView.SelectedCells [0] .Value = exampleString;
DataGridView相应更新,点击保存 - 更新执行时不用挂,但新值显然是不承诺SQL Server,因为一旦加载相同的表,它再次显示旧值。
任何想法,我在这里忘记了什么?每个输入都是赞赏!
dataGridView.EndEdit();
bSource.EndEdit();
dAdapter.Update(dTable);
工作!
经过漫长的搜索我终于找到了一个线索:
Kudos - >
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!
dataGridView.EndEdit();
bSource.EndEdit();
dAdapter.Update(dTable);
Works!
After a lengthy search I finally found a clue:
Kudos -> .NET WinForms End Current Edit
这篇关于DataGrid使用SQLDataAdapter更新问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!