try
{
OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
"Dsn=mdc;" +
"Uid=root;" +
"Pwd=;";
OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET Address = @Address, AssetNumber = @AssetNumber, Contact = @Contact, DRNumber = @DRNumber, InvoiceNumber = @InvoiceNumber, ItemQty = @ItemQty, PONumber = @PONumber, ReceivedBy = @ReceivedBy, Supplier = @Supplier WHERE No = @No;", conn);
//DataRowView drv = (DataRowView) comboBox1.SelectedValue;
cmd.Parameters.AddWithValue("@No", noTextBox.Text);
cmd.Parameters.AddWithValue("@AssetNumber", assetNumberTextBox.Text);
cmd.Parameters.AddWithValue("@Contact", contactTextBox.Text);
cmd.Parameters.AddWithValue("@DRNumber", dRNumberTextBox.Text);
cmd.Parameters.AddWithValue("@InvoiceNumber", invoiceNumberTextBox.Text);
cmd.Parameters.AddWithValue("@ItemQty", itemQtyTextBox.Text);
cmd.Parameters.AddWithValue("@PONumber", pONumberTextBox.Text);
cmd.Parameters.AddWithValue("@ReceivedBy", receivedByTextBox.Text);
cmd.Parameters.AddWithValue("@Supplier", supplierTextBox.Text);
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();
MessageBox.Show("update successful");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
每当我从datagridview点击一个数据并使用文本框更改它时,它就不会更新。
答案 0 :(得分:0)
您希望使用OdbcDataAdapter和更新命令填充DataTable。
编辑:
以下是代码中的剪切:
//here your cmd is set to UPDATE
OdbcCommand cmd = new OdbcCommand("UPDATE tbl_delivery SET Address = @Address, AssetNumber = @AssetNumber, Contact = @Contact, DRNumber = @DRNumber, InvoiceNumber = @InvoiceNumber, ItemQty = @ItemQty, PONumber = @PONumber, ReceivedBy = @ReceivedBy, Supplier = @Supplier WHERE No = @No;", conn);
//here your SelectCommand is set to UPDATE
ds.SelectCommand = cmd;
//here you fill your datatable
ds.Fill(dtable);
您应该正确设置SelectCommand和UpdateCommand。您无法使用UPDATE填充数据表。
答案 1 :(得分:0)
如果您的查询提供了预期的结果,并且数据在当时的数据库中得到更新。然后你需要ReBind
网格将更新的数据加载到网格中。
让Gv_delivery
成为您的数据网格,然后代码如下:
Gv_delivery.Rebind
执行更新后要附加到代码的代码。