2

I would like to know how to call an event in c#. Practically I have a datagridview double click event which populates textboxes of f2 with values of a selected row in datagridview and shows form2 with these values in their assigned textboxes. Now I would like to do that with a click of a button, say call my datagridview double click event when that button is clicked, below is my double click event ty.

private void kryptonDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        frmUpdate f2 = new frmUpdate();
        f2.txtboxClearingAgent.Text = kryptonDataGridView1.SelectedRows[0].Cells["Clearing Agent Name"].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.txtboxTotalDepo.Text = kryptonDataGridView1.SelectedRows[0].Cells["Total Deposit"].Value.ToString();
        f2.txtboxAccountBal.Text = kryptonDataGridView1.SelectedRows[0].Cells["Account Balance"].Value.ToString();

        f2.Show();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }
}

private void kryptonbtnEdit_Click(object sender, EventArgs e)
{
    //using (frmUpdate frmUpdate = new frmUpdate())
    //{
    //    DialogResult result = frmUpdate.ShowDialog();
    //}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RichieCr7
  • 158
  • 1
  • 6
  • 15

1 Answers1

3

Since you are not using anything related to sender object and event args then the solution is as simple as this

kryptonDataGridView1_CellDoubleClick(null, null);

the method kryptonDataGridView1_CellDoubleClick is just a function like all other functions in C# and you can call it explicitly.

if you want more control you can do it like

private void kryptonbtnEdit_Click(object sender, EventArgs e)
{
    //set parameters of your event args
    var eventArgs = new DataGridViewCellEventArgs(yourColumnIndex, yourRowIndex);

    // or setting the selected cells manually before executing the function
    kryptonDataGridView1.Rows[yourRowIndex].Cells[yourColumnIndex].Selected = true;

    kryptonDataGridView1_CellDoubleClick(sender, eventArgs);
}

Note that events can only be raised from code within the control that declares the event. This does not fire the CellDoubleClick event, it just execute the function kryptonDataGridView1_CellDoubleClick that you register it to be executed when CellDoubleClick event fires. If you have registered other methods to be invoked when CellDoubleClick fired then you should execute them too explicitly.

Keep in mind that you can always create a derived class from KryptonDataGridView and handle these things internally and provide an API for yourself to use it later or in many complex scenarios you can get the underlying method which fires the event internally in the control using reflection and fire it manually.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74