我有一个将数据保存在数据库中的数据网格视图,但删除和更新不起作用

I have a datagrid view which saves data in the database but the delete and update not working

我有一个带有数据网格视图和三个按钮的表单,AddNew、Save、Remove

当我点击 AddNew 按钮时,会添加一个新行,为此使用的代码是:

 this.netWeightMasterDataBindingSource.AddNew();

所以事实证明数据表适配器具有执行删除、插入和更新的属性,所以我决定走那条路:

插入按钮

this.net_Weight_Master_DataTableAdapter.Insert(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
                net_Weight_Master_DataDataGridView.Refresh();
MessageBox.Show("Record Inserted");

删除按钮

   //Update button update dataset after insertion,upadtion or deletion
            DialogResult dr = MessageBox.Show("Are you sure to delete the record", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                this.net_Weight_Master_DataTableAdapter.Delete(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
                net_Weight_Master_DataDataGridView.Refresh();
                MessageBox.Show("Record Deleted");
            }
            if (dr == DialogResult.No)
            {
                Close();
            }

删除Sql

         DELETE FROM [dbo].[Net Weight Master Data] 
        WHERE (([Unit UPC Base Item] = @Original_Unit_UPC_Base_Item) 
       AND ([Production Line] = @Original_Production_Line) 
       AND ((@IsNull_Preset_Number = 1 AND [Preset Number] IS NULL) 
       OR ([Preset Number] = @Original_Preset_Number)) 
       AND ((@IsNull_Package_Type = 1 
      AND [Package Type] IS NULL) OR ([Package Type] = @Original_Package_Type)) 
      AND ((@IsNull_Weight_Factor = 1 
      AND [Weight Factor] IS NULL) OR ([Weight Factor] = @Original_Weight_Factor))  AND ((@IsNull_Piece = 1 AND [Piece] IS NULL) OR ([Piece] = @Original_Piece)) 
    AND ((@IsNull_Units_Per_Carton = 1 AND [Units Per Carton] IS NULL) OR ([Units Per Carton] = @Original_Units_Per_Carton)) 
    AND ((@IsNull_Pcs_Per_Unit = 1 AND [Pcs Per Unit] IS NULL) OR ([Pcs Per Unit] = @Original_Pcs_Per_Unit)) 
    AND ((@IsNull_Upper_Limit_Unit = 1 AND [Upper Limit Unit] IS NULL) OR ([Upper Limit Unit] = @Original_Upper_Limit_Unit)) 
    AND ((@IsNull_Upper_Limit_Factor = 1 AND [Upper Limit Factor] IS NULL) OR ([Upper Limit Factor] = @Original_Upper_Limit_Factor)) 
    AND ((@p3 = 1 AND [Label Wt (g)] IS NULL) OR ([Label Wt (g)] = @p2)) AND ((@p6 = 1 AND [Tare Wt (g)] IS NULL) OR ([Tare Wt (g)] = @p5)) AND ((@p9 = 1 
    AND [Constant Tare Wt (g)] IS NULL) OR ([Constant Tare Wt (g)] = @p8)) AND ((@p12 = 1 AND [Tare Variation Factor (g)] IS NULL) OR ([Tare Variation Factor (g)] = @p11)) 
    AND ((@p15 = 1 AND [Pkg Length (mm)] IS NULL) OR ([Pkg Length (mm)] = @p14)) AND ((@IsNull_Film_Product_Code = 1 
AND [Film Product Code] IS NULL) OR ([Film Product Code] = @Original_Film_Product_Code)) AND ((@p18 = 1 
AND [Film Width (mm)] IS NULL) OR ([Film Width (mm)] = @p17)) AND ((@p21 = 1 AND [Forming Tube (mm)] IS NULL) OR ([Forming Tube (mm)] = @p20)) AND ((@IsNull_Type_of_Jaws = 1 
AND [Type of Jaws] IS NULL) OR ([Type of Jaws] = @Original_Type_of_Jaws)) 
AND ((@IsNull_Last_Updated = 1 AND [Last Updated] IS NULL) OR ([Last Updated] = @Original_Last_Updated)) AND ((@IsNull_Comments = 1 AND [Comments] IS NULL) OR ([Comments] = @Original_Comments)) AND ((@IsNull_Field1 = 1 AND [Field1] IS NULL) OR ([Field1] = @Original_Field1)))

更新按钮

 //Update button update dataset after insertion,upadtion or deletion
         DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
         if (dr == DialogResult.Yes)
        {
            this.net_Weight_Master_DataTableAdapter.Update(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
            net_Weight_Master_DataDataGridView.Refresh();
            MessageBox.Show("Record Updated");
        }  

我在使用插入和删除按钮时遇到错误,我得到的错误是方法的重载需要 1 个参数

我们可以使用替代方法并在每次刷新数据库的操作后更新数据集。

插入记录

this.Validate();
            this.netWeightMasterDataBindingSource.EndEdit();
            this.net_Weight_Master_DataTableAdapter.Update(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
            //this.tableAdapterManager.UpdateAll(this.corsicanaNetWeightDataSet);
            MessageBox.Show("Record Inserted");

删除记录

   DialogResult dr = MessageBox.Show("Are you sure to delete the record", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
    if (dr == DialogResult.Yes)
    {
        this.netWeightMasterDataBindingSource.RemoveCurrent();
        this.netWeightMasterDataBindingSource.EndEdit();
        this.net_Weight_Master_DataTableAdapter.Update(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
        net_Weight_Master_DataDataGridView.Refresh();
        MessageBox.Show("Record Deleted");
    }
    if (dr == DialogResult.No)
    {
        Close();
    }

更新记录

DialogResult dr = MessageBox.Show("Are you sure to save Changes", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
         if (dr == DialogResult.Yes)
        {
            this.net_Weight_Master_DataTableAdapter.Update(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
            net_Weight_Master_DataDataGridView.Refresh();
            MessageBox.Show("Record Updated");
        }