“数据绑定删除”按钮

本文关键字:按钮 删除 数据绑定 | 更新日期: 2023-09-27 18:37:20

我创建了一个表单,其中有 4 个文本框和开箱即用的绑定导航器。我正在显示单个表中的数据,我想让删除按钮工作......而我不能。这是我用来将数据从数据库刷新/添加到我的绑定源和数据源(我在表单加载中调用)的方法:

public void Fill_DataSource()
      {
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRDBConnection"].ConnectionString);
          try
          {
              conn.Open();
              SqlDataAdapter da1 = new SqlDataAdapter(new SqlCommand("select * from BusinessGroups", conn));
              DataSet ds = new DataSet();
              da1.Fill(ds);
              BGbindSource.ResetBindings(false);
              BGbindSource.DataSource = ds.Tables[0];
              bindingNavigator1.BindingSource = BGbindSource;
              //BusinessGroupCode
              textBox1.DataBindings.Clear();
              textBox1.DataBindings.Add(new Binding("Text", this.BGbindSource, "BusinessGroupCode", true, DataSourceUpdateMode.OnPropertyChanged));
              //BusinessGroupName
              textBox2.DataBindings.Clear();
              textBox2.DataBindings.Add(new Binding("Text", this.BGbindSource, "BusinessGroupName", true, DataSourceUpdateMode.OnPropertyChanged));
              //BusinessGroupDesc
              textBox3.DataBindings.Clear();
              textBox3.DataBindings.Add(new Binding("Text", this.BGbindSource, "BusinessGroupDescription", true, DataSourceUpdateMode.OnPropertyChanged));
              //BusinessGroupId
              textBox4.DataBindings.Clear();
              textBox4.DataBindings.Add(new Binding("Text", this.BGbindSource, "BGId", true, DataSourceUpdateMode.OnPropertyChanged));
          }
          catch (Exception)
          {
              toolStripStatusLabel3.Text = "Database Is Offline or the Connection is not set correctly!";
          }
          finally
          {
              conn.Close();
          }
      }

这是删除按钮背后的代码:

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
      {
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRDBConnection"].ConnectionString);
          try
          {
              conn.Open();
              SqlCommand sqlcmd = new SqlCommand("delete from BusinessGroups where BGId=@BGId",conn);
              //BGId
              SqlParameter param4 = new SqlParameter("@BGId", SqlDbType.Int);
              if (textBox4.Text.Trim() == "")
              {
                  param4.Value = -999;
              }
              else
              {
                  param4.Value = textBox4.Text;
              }
              sqlcmd.Parameters.Add(param4);
              try
              {
                  sqlcmd.ExecuteNonQuery();
                  //MessageBox.Show("Successfully deleted!");
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString(), "Error! Could not delete the requested information!", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              //Fill_DataSource();
              BGbindSource.ResetBindings(true);

          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message, "Error! Could not delete the requested information!", MessageBoxButtons.OK, MessageBoxIcon.Error);
              //throw;
          }
          finally
          {
              if (conn != null) conn.Close();
          }
      }

如果我按下它,它将删除绑定中的上一项,更准确地说,使用断点,我注意到 BGId 参数始终取上一个 BGId 的值,我的意思不是我当前定位的那个,而是前一个。为什么会发生这种情况,我该如何解决?多谢!已编辑:为了回答您的问题,以下是保存按钮后面的代码,该按钮按预期工作:

private void bindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
          if (BGbindSource.Current == null) return;
          if (textBox1.Text.Trim() == "")
          {
              MessageBox.Show("Business Group Code cannot be blank!");
              textBox1.Focus();
              return;
          }
          if (textBox2.Text.Trim() == "")
          {
              MessageBox.Show("Business Group Name cannot be blank!");
              textBox2.Focus();
              return;
          }
          SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SRDBConnection"].ConnectionString);
          try
          {
              conn.Open();
              SqlCommand sqlcmd = new SqlCommand("if exists (select 1 from BusinessGroups where BGId=@BGId) "
                  + " update BusinessGroups "
                  + " set BusinessGroupCode=@BGCode, "
                  + " BusinessGroupName=@BGName, "
                  + " BusinessGroupDescription=@BGDesc, "
                  + " UpdateTimeStamp= getdate()"
                  + " where BGId=@BGId "
                  + " else "
                  + " insert into BusinessGroups (BusinessGroupCode,BusinessGroupName,BusinessGroupDescription) "
                  + " select @BGCode,@BGName,@BGDesc " 
                  , conn);
              //BGCode
              SqlParameter param1 = new SqlParameter("@BGCode", SqlDbType.NVarChar, 30 );
              param1.Value = textBox1.Text;
              sqlcmd.Parameters.Add(param1);
              //BGName
              SqlParameter param2 = new SqlParameter("@BGName", SqlDbType.NVarChar, 150);
              param2.Value = textBox2.Text;
              sqlcmd.Parameters.Add(param2);
              //BGDesc
              SqlParameter param3 = new SqlParameter("@BGDesc", SqlDbType.NVarChar, 1000);
              param3.Value = textBox3.Text;
              sqlcmd.Parameters.Add(param3);
              //BGId
              SqlParameter param4 = new SqlParameter("@BGId", SqlDbType.Int);
              if (textBox4.Text.Trim() == "")
              {
                  param4.Value = -999;
              }
              else 
              {
                  param4.Value = textBox4.Text;
              }
              sqlcmd.Parameters.Add(param4);
              sqlcmd.ExecuteNonQuery();
              Fill_DataSource();
              //BGbindSource.ResetBindings(false);
              MessageBox.Show("Successfully saved!");
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message,"Error! Could not save the requested information!",MessageBoxButtons.OK,MessageBoxIcon.Error);
              throw;
          }
          finally
          {
              if (conn != null)  conn.Close();
          }
      }

“数据绑定删除”按钮

要使用BindingNavigator的默认删除按钮删除行,您不需要任何代码。因此,只需在bindingNavigatorDeleteItem_Click中删除您自己的代码,它应该可以工作。