DataBindings删除按钮

时间:2013-10-29 08:49:50

标签: c# winforms textbox bindingnavigator

我是C#的新手,所以请光临我。好的,所以我创建了一个表单,其中我有4个文本框和开箱即用的bindingnavigator。我正在显示单个表中的数据,我想让删除按钮工作......我不能。 这是我用来刷新/添加数据从db到我的bindingsource和数据源(我在表单的加载中调用)的方法:

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();
          }
      }

1 个答案:

答案 0 :(得分:0)

要使用BindingNavigator上的默认删除按钮删除行,您不需要任何代码。因此,只需在bindingNavigatorDeleteItem_Click中删除自己的代码即可。