0

我有一个带有 3 个 Gridviews 的 Windows 窗体(见屏幕截图)在此处输入图像描述。其中一个 gridviews 在一个空单元格中获得一个新值,另外两个分别获得一个新行。单击保存按钮时,我想更新添加了值的表并将新行插入到其他两个表中。我很难找出正确的代码。现在我有:

private void buttonSave_Click(object sender, EventArgs e)
{
   DataTable table1 = alereDataSet.Tables["immaster"];
   DataTable table2 = uPCDataSet.Tables["UPC"];
   DataTable table3 = hangtagDataSet.Tables["upccode"];
   DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
   if (dr == DialogResult.Yes)
   try
   {
      immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.CurrentRows));
      upccodeTableAdapter.Update(table3.Select(null, null, DataViewRowState.Added));
      uPCTableAdapter.Update(table2.Select(null, null, DataViewRowState.Added));
   }

   catch (System.Exception ex)
   {
      System.Windows.Forms.MessageBox.Show(ex.Message);
   }
}

但是当我点击保存时没有任何反应。我有一种感觉,这是因为我的表格更新方法。三个表中的两个是 VFP 表,第三个是 SQL 表。我需要帮助的是弄清楚更新命令。这两个 VFP 表不会自动创建 Update 方法,SQL 表的代码是:

UPDATE UPC
SET UPCBarcode = @UPCBarcode, UPCNumber = @UPCNumber, ItemNumber = @ItemNumber, Itemdescrip = @Itemdescrip
WHERE (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND 
                      (ItemNumber IS NULL) AND (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND 
                      (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND 
                      (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND 
                      (Itemdescrip = @Original_Itemdescrip)

不知道我还应该发布什么。请询问您认为可能相关的更多代码。

4

1 回答 1

0

好吧,我想在一周没有任何事情之后,我终于能够弄清楚了。更新 SQL 表相当容易,因为它在创建数据集时创建了自己的 UPDATE 方法。对于 VFP 表,我必须编写自己的 UPDATE 命令,第一个表是

    UPDATE    immaster
    SET       item = ?, descrip = ?, upccode = ?
    WHERE     (item = ?)

第二个 VFP 表是

UPDATE    upccode
SET              item = ?, upccode = ?, memoupc = ?
WHERE     (item = ?)

然后我必须为 Save 按钮编写事件处理程序:

//Save the data from the gridviews back to the respective tables
    private void buttonSave_Click(object sender, EventArgs e)
    {
        //Declare which tables get updated
        DataTable table1 = alereDataSet.Tables["immaster"];
        DataTable table2 = uPCDataSet.Tables["UPC"];
        DataTable table3 = hangtagDataSet.Tables["upccode"];
        DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        if (dr == DialogResult.Yes)
            try
            {
                //Update immaster with the modified data
                immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.ModifiedCurrent));
                dataGridView1.Refresh();
                //Insert a new row in the upccode table with the values from the cells in the last row
                upccodeTableAdapter.Insert(dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[1].Value.ToString(), ("Added " + System.DateTime.Now));
                dataGridView3.Refresh();
                //Insert a new row in UPC table with the values from the cells in the last row
                uPCTableAdapter.Insert(dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[3].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[2].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[1].Value.ToString());
                dataGridView2.Refresh();
                clearTextboxes();
            }
            catch (SqlException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
    }
于 2013-11-08T20:48:20.440 回答