我有这个代码,我正在尝试从Windows应用程序中的C#gridview更改中更新数据库....
在更新按钮单击事件...
中声明适配器选择命令时会出现此问题我不确定使用哪个select命令或更新命令,以便更新数据库表中的datagridview更改....
有人能指出我正确的方向来解决这个问题吗?
public partial class KnowledgeBaseForm : Form
{
private SqlDataAdapter SDA = new SqlDataAdapter();
private void button_retrievekb_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlDataAdapter SDA = new SqlDataAdapter(@"SELECT * From Table1", con);
DT = new DataTable();
SDA.Fill(DT);
bindingsource.DataSource = DT;
dataGridView.DataSource = bindingsource;
if (DT.Rows.Count > 0)
{
dataGridView.Columns[0].DefaultCellStyle.ForeColor = Color.Gray;
dataGridView.Columns[0].ReadOnly = true;
}
else
{
MessageBox.Show("No Knowledge Base Rules Found");
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
finally
{
con.Close();
}
}
}
private void button_update_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//binding the datasource with the changes made in the gridview
bindingsource.DataSource = dataGridView.DataSource;
SDA.SelectCommand = (//some necessary command or NULL update command which needs to update my database);
scb = new SqlCommandBuilder(SDA);
SDA.Update((DataTable) bindingsource.DataSource);
MessageBox.Show("Updates successfully submitted to CoSD");
}
}
答案 0 :(得分:0)
您可能需要考虑在对与其关联的数据源(例如DataSource
)进行任何更改后更新网格的bindingSource
属性。
可能类似以下内容:
SDA.Update((DataTable) bindingsource.DataSource);
// After updating everything, rebind your grid
dataGridView.DataSource = bindingsource.DataSource;