请帮我解决这个问题,因为我几乎整整一天都在努力如何在绑定导航器中正确定义保存按钮。我是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();
}
}