我创建了一个表单,其中有 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();
}
}