我想知道如何在 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 供您自己稍后使用,或者在许多复杂的场景中,您可以获取底层方法,该方法使用反射在控件内部触发事件并手动触发它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)