datagridview更新到sql数据库

时间:2014-11-13 06:04:38

标签: sql-server database datagridview

我在使用datagridview更新sql中的表时遇到问题。

这是我如何将数据从sql调用到我的datagrid:

        string strsql = "select a.Denomination, a.Value, b.Quantity, b.Amount from [Masterfile].[Denomination] a "+
        "left join [GEARS-POS].[POS].[CashCount] b "+
        "on A.Sequence = B.Sequence order by a.sequence";
        dataGridViewEx1.AutoGenerateColumns = false;
        dataGridViewEx1.AllowUserToDeleteRows = false;
        dataGridViewEx1.AllowUserToAddRows = false;
        DataTable dtgt = GearsLibrary.Gears.GetData(strsql, Common.Common.ConnectionString());
        BindingSource bSource = new BindingSource();
        bSource.DataSource = dtgt;

        dataGridViewEx1.DataSource = bSource;

        dataGridViewEx1.Columns[0].DataPropertyName = "Denomination";
        dataGridViewEx1.Columns[1].DataPropertyName = "Value";
        dataGridViewEx1.Columns[2].DataPropertyName = "Quantity";
        dataGridViewEx1.Columns[3].DataPropertyName = "Amount";

以下是我使用按钮点击事件更新数据库的方法:

        string con = "Data Source=WEBSERVER;Initial Catalog=GEARS-POS;User id=sa;Password=123456";
        string strsql = "select a.Denomination, a.Value, b.Quantity, b.Amount from [Masterfile].[Denomination] a " +
        "left join [GEARS-POS].[POS].[CashCount] b " +
        "on A.Sequence = B.Sequence order by a.sequence";
        SqlDataAdapter da = new SqlDataAdapter(strsql, con);
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        DataTable dt = new DataTable();

        da.Update(dt);

每当我点击更新按钮时,都没有任何反应。它不会更新我的表或任何东西。 :/

1 个答案:

答案 0 :(得分:0)

我注意到你使用了名为" dtgt"作为数据源,当您想要更新数据时,您使用另一个Datatable Called Dt,因此您应该对同一个dataSource进行更新。 而不是写:

da.Update(dt);

//You must write:
da.Update(dtgt);