RichieCr7
RichieCr7

Reputation: 158

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

i have an update form which opens as showDialog. i notice when i click on save my update process goes through, closes form and refresh datagrid on parent form and when cancel button is click it closes form and also refreshes datagrid. how do i prevent datagrid from refresh when cancel button is clicked, am thinking of doing a public int declaration to return 1 when save is clicked and 0 when cancel is clicked but can't figure out how to do so. below is code for calling update form from parent form

   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);

    }

and inside update form which displays as dialog to parent form i tried something like that

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

dialog result from save click method

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

Upvotes: 0

Views: 126

Answers (1)

Steve
Steve

Reputation: 216353

The return value of a call to Form.ShowDialog is a DialogResult value. This value is taken by the Form engine directly from the DialogResult property of the Button clicked. If the button has any value for this property different from DialogResult.None then this button will close the form and its DialogResult property is returned by the call to ShowDialog()

So if you have a button on your frmUpdate with its DialogResult set to OK then

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

Of course you don't need to handle yourself the closing process of the frmUpdate form. It is automatically handled by the Form engine

Upvotes: 0

Related Questions