【问题标题】:I have a datagrid view which saves data in the database but the delete and update not working我有一个数据网格视图,它将数据保存在数据库中,但删除和更新不起作用
【发布时间】:2015-01-22 21:27:08
【问题描述】:

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

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

 this.netWeightMasterDataBindingSource.AddNew();

原来 datatableadapter 具有执行删除、插入和更新的属性,所以我决定走这条路:

插入按钮

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 个参数

【问题讨论】:

  • 当您单击删除并从网格中删除它时。删除的实际代码在哪里......以及重新绑定数据网格的调用......?
  • 你必须更新 DataTableAdapter 删除行后,像这样在你的网格上反映它this.netWeightMasterDataBindingSource.RemoveCurrent(); this.netWeightMasterDataBindingSource.EndEdit(); this.net_Weight_Master_DataTableAdapter.Update(this.corsicanaNetWeightDataSet.Net_Weight_Master_Data);
  • @MethodMan 我在问题中添加了更多代码有帮助吗?请帮忙!!
  • @prasy 我在问题中添加了更多代码,请帮助!!
  • @MethodMan :0 享受米勒时间,一旦我解决了这个令人生畏的问题,我就可以享受米勒时间了 :)

标签: c# sql-server-2008 datagridview


【解决方案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");
        }

【讨论】:

  • 它有效,但唯一的问题是,如果用户一次删除、插入和更新多行,并且一旦所有更改都反映在数据库上,只需单击更新、插入或删除按钮。我也在考虑做同样的事情。感谢您的帮助!。
猜你喜欢
  • 2017-12-05
  • 2011-09-21
  • 2016-06-22
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多