数据表不接受在网格视图中进行的更新以更新数据库

时间:2016-04-05 17:16:02

标签: c# winforms gridview datatable windows-applications

我有以下代码,我正在尝试从Windows应用程序中的c#网格视图更改中更新数据库....

代码没有更新SQL Server数据库......我不确定问题究竟发生在哪里。一切似乎都很好......

或者有没有办法更新数据库表,再次使用“更新查询语句”,重点是如何为数据网格视图中的任何值更改编写udate语句?

我认为绑定和select命令语句可能存在问题......

有人能指出我正确的方向来解决这个问题吗?

public partial class KnowledgeBaseForm : Form
{
    private SqlDataAdapter SDA = new SqlDataAdapter();
    private DataTable DT = new DataTable();

    private void button_retrievekb_Click(object sender, EventArgs e)
    {
        try
        {
            con.Open();
            SqlDataAdapter SDA = new SqlDataAdapter(@"SELECT * From Table1", con);

            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;

            scb = new SqlCommandBuilder(SDA);                 
            SDA.Update((DataTable) bindingsource.DataSource);

            MessageBox.Show("Updates successfully submitted to CoSD");
        }
    }
}

0 个答案:

没有答案