单击取消按钮时如何实现功能 return 0 但单击保存时 return 1 并刷新网格

how to implement a function when cancel button is clicked return 0 but when save is clicked return 1 and refresh grid

我有一个以 showDialog 打开的更新表单。我注意到当我点击保存我的更新过程时,关闭表单并刷新父表单上的数据网格,当单击取消按钮时它关闭表单并刷新数据网格。单击取消按钮时如何防止数据网格刷新,我正在考虑在单击保存时对 return 1 进行 public int 声明,在单击取消时为 0 但无法弄清楚如何这样做。下面是从父表单调用更新表单的代码

   private void kryptonDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            frmUpdate f2 = new frmUpdate();


            f2.lblClientID.Text = kryptonDataGridView1.SelectedRows[0].Cells["ClientID"].Value.ToString();
            f2.lblClearinAgentID.Text = kryptonDataGridView1.SelectedRows[0].Cells["Clearing_Agent_ID"].Value.ToString();
            f2.textboxClientCode.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Code"].Value.ToString();
            f2.txtboxClientName.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Name"].Value.ToString();
            f2.txtboxPostalAddress.Text = kryptonDataGridView1.SelectedRows[0].Cells["Postal Address"].Value.ToString();
            f2.txtboxTelephone.Text = kryptonDataGridView1.SelectedRows[0].Cells["Telephone"].Value.ToString();
            f2.txtboxFax.Text = kryptonDataGridView1.SelectedRows[0].Cells["Fax"].Value.ToString();
            f2.txtboxEmailAddress1.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 1"].Value.ToString();
            f2.txtboxEmailAddress2.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 2"].Value.ToString();
            f2.txtboxEmailAddress3.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 3"].Value.ToString();
            f2.txtboxWebsite.Text = kryptonDataGridView1.SelectedRows[0].Cells["Website"].Value.ToString();
            f2.txtboxChargeRate.Text = kryptonDataGridView1.SelectedRows[0].Cells["Charge Rate"].Value.ToString();
            //f2.lblTotalDeposit.Text = kryptonDataGridView1.SelectedRows[0].Cells["Total Deposit"].Value.ToString();
            //f2.lblAccountBal.Text = kryptonDataGridView1.SelectedRows[0].Cells["Account Balance"].Value.ToString();

            f2.ShowDialog();
            kryptonbtnDelete.Enabled = false;

   private void kryptonbtnEdit_Click(object sender, EventArgs e)
    {

        kryptonDataGridView1_CellDoubleClick(null, null);

    }

以及显示为父表单对话框的更新表单内部我尝试了类似的东西

      private void kryptonCancel_Click(object sender, EventArgs e)
    {
        frmUpdate_FormClosing(null,null);
    }

    private void frmUpdate_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (DialogResult != DialogResult.OK)
        {
            return;
        }
        else
        {
            e.Cancel = true;
        }
    }

保存点击方法的对话框结果

      DialogResult result = MessageBox.Show("Do you want to Update this Client?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    MessageBox.Show("Client information successfully Updated", "Updating Client(s) Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    int rowsUpdated = cmd.ExecuteNonQuery();
                    if (rowsUpdated > 0)
                    {
                    }
                }

                else if (result == DialogResult.No)
                {

                    return;
                }

调用 Form.ShowDialog 的 return 值是一个 DialogResult 值。该值由 Form 引擎直接从单击的 Button 的 DialogResult 属性 中获取。如果按钮对此 属性 有任何不同于 DialogResult.None 的值,则此按钮将关闭表单,并且其 DialogResult 属性 是 return 通过调用 ShowDialog()

因此,如果您的 frmUpdate 上有一个按钮,其 DialogResult 设置为 OK,那么

if(f2.ShowDialog() == DialogResult.OK))
  // User hits the OK button, refresh
else
  // No refresh here...

当然您不需要自己处理frmUpdate 表单的关闭过程。由表单引擎自动处理