如何更新datagridview中的数据?

时间:2014-08-08 02:49:09

标签: c# datagridview

                   OdbcConnection conn = new OdbcConnection();

           conn.ConnectionString =
                  "Dsn=mdc;" +
                  "Uid=root;" +
                  "Pwd=;";


           OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET (Supplier, InvoiceNumber, DRNumber, PONumber, ItemQty, ReceivedDate, Address, Contact, ReceivedBy, AssetNumber) (Supplier ='" + this.supplierTextBox.Text + "',InvoiceNumber ='" + this.invoiceNumberTextBox.Text + "',DRNumber ='" + this.dRNumberTextBox.Text + "',PONumber ='" + this.pONumberTextBox.Text + "',ItemQty ='" + this.itemQtyTextBox.Text + "',ReceivedDate ='" + this.receivedDateDateTimePicker.Text + "',Address ='" + this.addressTextBox.Text + "',Contact ='" + this.contactTextBox.Text + "',ReceivedBy ='" + this.receivedByTextBox.Text + "',AssetNumber ='" + this.assetNumberTextBox.Text + "'", conn);

              cmd.CommandType = CommandType.Text;

            OdbcDataAdapter ds = new OdbcDataAdapter(cmd);

             ds.SelectCommand = cmd;

            System.Data.DataTable dtable = new System.Data.DataTable();

             ds.Fill(dtable);

            tbl_deliveryDataGridView.DataSource = dtable;
             conn.Open();
            cmd.ExecuteNonQuery();

更新按钮无法正常工作,请检查我的更新声明是否正确。我正在使用c#..............

1 个答案:

答案 0 :(得分:1)

我认为您的查询不正确。

  OdbcConnection conn = new OdbcConnection();

       conn.ConnectionString =
              "Dsn=mdc;" +
              "Uid=root;" +
              "Pwd=;";


       OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET Supplier ='" + this.supplierTextBox.Text + "',InvoiceNumber ='" + this.invoiceNumberTextBox.Text + "',DRNumber ='" + this.dRNumberTextBox.Text + "',PONumber ='" + this.pONumberTextBox.Text + "',ItemQty ='" + this.itemQtyTextBox.Text + "',ReceivedDate ='" + this.receivedDateDateTimePicker.Text + "',Address ='" + this.addressTextBox.Text + "',Contact ='" + this.contactTextBox.Text + "',ReceivedBy ='" + this.receivedByTextBox.Text + "',AssetNumber ='" + this.assetNumberTextBox.Text + "'", conn);

那你的 Where子句在哪里?只需将其添加到查询即可 然后

cmd.ExecuteNonQuery();
ds= newodbcDataAdapter(cmd);
ds.Fill(dtable);
tbl_deliveryDataGridView.ItemsSource = dtable.DefaultView;

在将数据显示到数据网格之前,最好先执行查询,以便能够查看更新的表格。