0

请帮助我解决这个问题,因为我几乎一整天都在努力解决如何在绑定导航器中正确定义保存按钮。我是 C# 新手,所以请多多包涵。

好的,所以我有一个简单的表格,我在表格中逐行显示。我添加了绑定导航器并创建了一个名为 SaveItem 的新按钮,以便我可以保存对当前显示的行所做的任何更改:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace MyApp
{
   public partial class DefineBusinessGroups : Form
   {
      BindingSource BGbindSource = new BindingSource();
      public string UserId { get; set; }
      public string UserName { get; set; }
      public string UserType { get; set; }
      public string BssGr { get; set; }

      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.DataSource = ds.Tables[0];
              bindingNavigator1.BindingSource = BGbindSource;

              //BusinessGroupCode
              textBox1.DataBindings.Add(
                  new Binding("Text", 
                              this.BGbindSource, 
                              "BusinessGroupCode", 
                              true, 
                              DataSourceUpdateMode.OnPropertyChanged));

              //BusinessGroupName
              textBox2.DataBindings.Add(
                  new Binding("Text", 
                              this.BGbindSource, 
                              "BusinessGroupName", 
                              true, 
                              DataSourceUpdateMode.OnPropertyChanged));

              //BusinessGroupDesc
              textBox3.DataBindings.Add(
                  new Binding("Text", 
                              this.BGbindSource, 
                              "BusinessGroupDescription", 
                              true, 
                              DataSourceUpdateMode.OnPropertyChanged));

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

      public DefineBusinessGroups()
      {
         InitializeComponent();
      }

      private void DefineBusinessGroups_Load(object sender, EventArgs e)
      {
         Fill_DataSource();
         toolStripStatusLabel3.Text = "UserName: " + this.UserName;
         toolStripStatusLabel4.Text = "UserType: " + this.UserType;
         toolStripStatusLabel5.Text = "Business Group: " + this.BssGr;
         this.Text = this.Text + " as " + this.UserType;

         if (UserType == "ADM")
         {
             textBox1.ReadOnly = false;
             textBox2.ReadOnly = false;
             textBox3.ReadOnly = false;

         }
         else
         {
             textBox1.ReadOnly = true;
             textBox2.ReadOnly = true;
             textBox3.ReadOnly = true;
             bindingNavigatorAddNewItem.Visible = false;
             bindingNavigatorDeleteItem.Visible = false;
             bindingNavigatorSaveItem.Visible = false;
         }

      }

      private void bindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
          if (BGbindSource.Current == null) return;

          SqlConnection conn = 
          new SqlConnection(
          ConfigurationManager.ConnectionStrings["SRDBConnection"].ConnectionString);
          try
          {
              conn.Open();
              SqlCommand sqlcmd = new SqlCommand("update BusinessGroups"
                  + "set BusinessGroupCode=@BGCode,"
                  + "BusinessGroupName=@BGName,"
                  + "BusinessGroupDescription=@BGDesc"
                  + "where BGId=@BGId", conn);
              //BGCode
              SqlParameter param1 = new SqlParameter();
              param1.ParameterName = "@BGCode";
              param1.Value = textBox1;
              sqlcmd.Parameters.Add(param1);

              //BGName
              SqlParameter param2 = new SqlParameter();
              param2.ParameterName = "@BGName";
              param2.Value = textBox2;
              sqlcmd.Parameters.Add(param2);

              //BGDesc
              SqlParameter param3 = new SqlParameter();
              param3.ParameterName = "@BGDesc";
              param3.Value = textBox3;
              sqlcmd.Parameters.Add(param3);

              //BGId
              SqlParameter param4 = new SqlParameter();
              param4.ParameterName = "@BGId";
              param4.Value = textBox4;
              sqlcmd.Parameters.Add(param4);

              //Execute the update
              sqlcmd.ExecuteNonQuery();
              MessageBox.Show("Update done!");

          }
          catch (Exception)
          {
              MessageBox.Show("Something went wrong!");
          }
          finally
          {
              conn.Close();
          }
      }
   }
}

当我按下保存按钮时,我会从 catch 分支获取消息文本。事实上,我希望它只保存当前选定行(BGId)中的信息。

谢谢!

稍后编辑:保存按钮的代码错误。这是正确的代码:

private void bindingNavigatorSaveItem_Click(object sender, EventArgs e)
      {
          //if (BGbindSource.Current == null) 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 "
                  + " 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();
              BGbindSource.ResetBindings(false);
              MessageBox.Show("Successfully saved!");

          }
          catch (Exception)
          {
              MessageBox.Show("Something went wrong!");
              throw;
          }
          finally
          {
              if (conn != null)  conn.Close();
          }
      }
4

0 回答 0