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

时间:2015-01-26 11:02:15

标签: c# datagrid

我有一个更新表单,以showDialog打开。我注意到当我点击保存我的更新过程完成后,关闭表单并刷新父表单上的datagrid,当取消按钮单击它关闭表单并刷新datagrid。如何在单击取消按钮时阻止数据网格刷新,我想在单击保存时执行公共int声明返回1,单击取消时返回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;
                }

1 个答案:

答案 0 :(得分:0)

对Form.ShowDialog的调用的返回值是DialogResult值。 Form引擎直接从单击的Button的DialogResult属性获取此值。如果该按钮具有与DialogResult.None不同的此属性的任何值,则此按钮将关闭该窗体,并且通过调用ShowDialog()返回其DialogResult属性

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

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

当然,您不需要自己处理frmUpdate表单的结束过程。它由表单引擎

自动处理