如何使用按钮取消更改
本文关键字:取消 按钮 何使用 | 更新日期: 2023-09-27 18:06:51
当我打开表单时,它会通过绑定源自动加载数据库。有一个按钮可以按文本框中的值对数据进行更改还有一个按钮可以取消这些更改(如果您在文本框中输入值并按"取消",它将恢复到原始数据)。
我不知道如何编写取消按钮的代码,我已经成功地完成了其他所有操作
我当前的修改代码(编辑:)是:
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.Data.OleDb;
namespace Eastern_Property_Owners_Club
{
public partial class Club_Record_Maintenance : Form
{
private OleDbConnection dbConn; // Connection object
private OleDbCommand dbCmd; // Command object
private string sConnection;
private string sql;
public Club_Record_Maintenance()
{
InitializeComponent();
try
{
// Construct an object of the OleDbConnection class
// to store the connection string representing
// the type of data provider (database) and the source (actual db).
//string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:'abc.mdb;Jet OLEDB:Database Password=password";
//OleDbConnection MyConn = new OleDbConnection(ConnStr);
// sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Student.mdb";
sConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" + "Data Source = Eastern_Property_Maintenance.mdb";
dbConn = new OleDbConnection(sConnection);
dbConn = new OleDbConnection(sConnection);
dbConn.Open();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
}
private void button3_Click(object sender, EventArgs e)
{
//exits and goes to main menu
MainMenu newForm = new MainMenu();
newForm.Show();
this.Close();
}
private void Club_Record_Maintenance_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'eastern_Property_MaintenanceDataSet2.Club' table. You can move, or remove it, as needed.
this.clubTableAdapter.Fill(this.eastern_Property_MaintenanceDataSet2.Club);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//declaring value to textboxes
string Name = txtName.Text;
string Address = txtAddress.Text;
string Phone = txtPhone.Text;
//declaring parameters in the SQL code
//Field[Name] = whatever is in txtName.Text and so on
//-----
//adds values from the textboxes into the database
sql = " UPDATE Club SET "
+ " CompanyName = @Name"
+ ","
+ " CompanyAddress = @Address"
+ ","
+ " CompanyPhone = @Phone"
+ " WHERE CompanyName = @Name; ";
dbCmd = new OleDbCommand(sql, dbConn);
//adding the perameters to ever place-holder in my sql
dbCmd.Parameters.AddWithValue("@Name", txtName.Text);
dbCmd.Parameters.AddWithValue("@Address", txtAddress.Text);
dbCmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
// Execute query
dbCmd.ExecuteNonQuery();
}
catch (System.Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
//a message to show the button has worked
MessageBox.Show("Club has been Changed! 'nRefresh the content by going back to the Main Menu");
}
private void btnCancel_Click(object sender, EventArgs e)
{
}
}
}
它工作得很好,但我不知道如何编写一个取消按钮
再创建三个全局变量和一个初始化为false的bool值,以便在检索数据时存储数据库中的值,如下所示检查数据检索方法中的bool值并存储这些值。
if (!oldValues)
{
// your code to store values from the database when the data is retrieved for the first time
// change oldValues to true
}
因此,当进行更改并存储在数据库中时,这些全局变量包含您的旧值。用户按下取消按钮,您使用这些变量使用更新查询来撤消更改,就像您在更新中使用的一样,但现在在取消按钮上单击事件处理程序…您还必须将bool再次更改为false,这样当再次检索数据时,它将以旧值存储在变量中。
编辑:在你的Form Load事件中设置:
bool oldValues = false; // that means there are not old values stored
创建全局变量:
string oldName;
string oldAddress;
string oldPhone;
现在添加一个名为Add Record Button
的按钮,并在单击时执行此操作:
if (!oldValues) //Checks that there are no old Values stored already
{
if (txtName.Text != "") // handles the case if you have allowed nulls in this field
{
oldName = txtName.Text;
}
if (txtAddress.Text != "") // handles the case if you have allowed nulls in this field
{
oldAddress = txtAddress.Text;
}
if (txtPhone.Text != "") // handles the case if you have allowed nulls in this field
{
oldPhone = txtPhone.Text;
}
oldValues = true; // change to true so that program knows that there are old values stored
txtName.Clear(); // Clear all text boxes on form so that user can enter new data
txtAddress.Clear();
txtPhone.Clear();
}
在取消按钮上执行此操作:
if (oldValues) // Check if old values are stored
{
if (oldName != "") // check if its not an empty string
{
txtName.Text = oldName;
}
else // if it is a empty string then Clear the text box
txtName.Clear();
if (oldAddress != "")
{
txtAddress.Text = oldAddress;
}
else
txtAddress.Clear();
if (oldPhone != "")
{
txtPhone.Text = oldPhone;
}
else
txtPhone.Clear();
oldValues = false; // change the oldValues flag to false so that old values can be stored again...
}