如何将验证应用于表单

时间:2016-01-19 07:06:45

标签: c#

我有一个要求,我需要在datagridview中为多个页面执行查找和替换 - 它工作正常。问题是,在我找到一个特定的单词并替换它后,如果我单击“查找和替换”按钮,则先前的查找和替换值将消失。

如何处理它?<​​/ p>

代码如下:

public string toFind = "";
public string toReplace = "";

private void btnFindandReplace_Click(object sender, EventArgs e)
{
    Form2 f = new Form2();
    f.cmbColumnCombo.DataSource = cmbList;
    f.ShowDialog();
    toFind = f.txtfind.Text;
    toReplace = f.txtreplace.Text;

    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
    {
        if (dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().ToLower().Contains(f.txtfind.Text.ToLower()))
        {
            if (!string.IsNullOrEmpty(f.txtfind.Text))
            {
                dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value =
                    dataGridView1.Rows[i].Cells[f.cmbColumnCombo.Text].Value.ToString().Replace(f.txtfind.Text, f.txtreplace.Text);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试在btnFindandReplace_Click方法中初始化toFind和toReplace变量。

答案 1 :(得分:0)

在按钮单击事件处理程序之外声明您的Form对象。这将使您的表单全局并在同一对象上运行。