如何通过点击按钮调用datagridview事件? [英] How to call a datagridview event with a click of a button?
问题描述
private void kryptonDataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
try
{
frmUpdate f2 = new frmUpdate();
f2.txtboxClearingAgent.Text = kryptonDataGridView1.SelectedRows [0] .Cells [清除代理名称]。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,空值);
方法 kryptonDataGridView1_CellDoubleClick
只是一个函数C#中的所有其他功能,您可以明确地调用它。
如果您想要更多的控件,可以像
private void kryptonbtnEdit_Click(object sender,EventArgs e)
{
//设置事件参数的参数args
var eventArgs = new DataGridViewCellEventArgs(yourColumnIndex, yourRowIndex);
//或在执行函数之前手动设置所选单元格
kryptonDataGridView1.Rows [yourRowIndex] .Cells [yourColumnIndex] .Selected = true;
kryptonDataGridView1_CellDoubleClick(sender,eventArgs);
}
请注意,事件只能从声明事件的控件内的代码引发。这不会触发 CellDoubleClick
事件,它只是执行函数 kryptonDataGridView1_CellDoubleClick
,注册它将执行 CellDoubleClick
事件被触发。如果您在 CellDoubleClick
触发时注册了其他方法,那么您应该明确地执行它们。
保持您可以随时从 KryptonDataGridView
中创建派生类,并在内部处理这些内容,并提供一个API,以便您以后使用它或在许多复杂场景中获得底层方法它使用反射在控件内部触发事件,并手动触发。
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();
//}
}
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.
这篇关于如何通过点击按钮调用datagridview事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!