如何通过单击按钮调用数据网格视图事件

本文关键字:数据网 网格 视图 事件 数据 调用 何通过 单击 按钮 | 更新日期: 2023-09-27 18:36:47

我想知道如何在 c# 中调用事件。实际上,我有一个 datagridview 双击事件,该事件使用 datagridview 中选定行的值填充 f2 的文本框,并在分配的文本框中显示具有这些值的 form2。现在我想通过单击一个按钮来做到这一点,假设在单击该按钮时调用我的 datagridview 双击事件,下面是我的双击事件 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();
    //}
}

如何通过单击按钮调用数据网格视图事件

由于您没有使用与发送方对象和事件参数相关的任何内容,因此解决方案就这么简单

kryptonDataGridView1_CellDoubleClick(null, null);

方法kryptonDataGridView1_CellDoubleClick只是一个函数,就像 C# 中的所有其他函数一样,你可以显式调用它。

如果你想要更多的控制,你可以这样做

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

请注意,只能从声明事件的控件中的代码引发事件。这不会触发 CellDoubleClick 事件,它只是执行函数kryptonDataGridView1_CellDoubleClick注册该函数以在事件触发时执行CellDoubleClick该函数。如果您已经注册了要在触发CellDoubleClick时调用的其他方法,则应过于显式地执行它们。

请记住,您始终可以从KryptonDataGridView创建派生类并在内部处理这些事情,并为自己提供一个 API 供以后使用,或者在许多复杂的方案中,您可以获取使用反射在控件内部触发事件的基础方法并手动触发它。