C# 如何使用按钮取消更改

C# 如何使用按钮取消更改,c#,visual-studio-2010,C#,Visual Studio 2010,当我打开表单时,它会通过绑定源自动加载数据库。 文本框中有一个按钮,可通过值对数据进行更改 还有一个按钮用于取消这些更改(如果您在文本框中输入了值,然后按“取消”,它将恢复为原始数据) 我不知道如何编写“取消”按钮的代码,我已经成功地完成了其他所有操作 我当前的更改代码(编辑:)是: { 它工作正常,但我不知道如何编写“取消”按钮。再生成三个全局变量,并将bool初始化为false,以便在检索数据时存储数据库中的值检查数据检索方法中的bool,如下所示并存储值 if (!oldValues) {

当我打开表单时,它会通过绑定源自动加载数据库。 文本框中有一个按钮,可通过值对数据进行更改 还有一个按钮用于取消这些更改(如果您在文本框中输入了值,然后按“取消”,它将恢复为原始数据)

我不知道如何编写“取消”按钮的代码,我已经成功地完成了其他所有操作

我当前的更改代码(编辑:)是:

{


它工作正常,但我不知道如何编写“取消”按钮。

再生成三个全局变量,并将bool初始化为false,以便在检索数据时存储数据库中的值检查数据检索方法中的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,这样,当数据它将把变量中的值作为旧值存储

编辑: 在表单加载事件中执行此操作:

bool oldValues = false; // that means there are not old values stored
使这些全局变量:

string oldName;
string oldAddress; 
string oldPhone;
现在添加一个名为
添加记录按钮
的按钮,并在单击该按钮时执行此操作:

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...
}    

我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。你想取消什么?你能在取消时重新加载数据库中的数据吗?因为数据尚未提交到数据库中?@G Siry我可以,但我不知道执行此操作的代码,我对c相当陌生,因为数据尚未提交到数据库中-也许他可以从数据库中重新加载数据?这基本上意味着不需要代码来存储旧的values进入全局变量检查代码数据已提交到数据库…,他使用了更新query@SyedFarjad Zia Zaidi,你能给我一个完整的代码示例吗,我有点迷路了对不起,@user3536490我想这是你的答案,如果是,请标记为答案:P
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...
}