如何在Button_Click中调用Datagridview Cell Click事件 [英] How to call Datagridview Cell Click Event in Button_Click

查看:420
本文介绍了如何在Button_Click中调用Datagridview Cell Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我很高兴今天能找到这个网站Julie作为一个整体的c#编程和编程的新手,我在Windows应用程序中有一个gridview,可以做三件事。我首先从数据库导入数据,然后在clickcell事件上它根据单元格上的数据执行一些计算有没有办法让我在一个按钮点击事件中完成所有这些,而不必单击每个单元格来进行我的计算?

Hi guys, I am very happy to find this site today am Julie a newbie to c# programming and programming as a whole, I have a gridview in a windows application that does three things from me .first I import data from a database, then on a clickcell event it performs some calculations based on the data on the cell is there a way for me to do all these in one button click event instead of having to click every cell just to do my calculations?

void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   //Do something
}button1_click(object sender Args e)
{
//how do I call gridview method in here??
}

推荐答案

你可以把代码放到那里你想在自己的方法中执行,然后从两个事件处理程序中调用它。当然,你必须自己在事件处理程序中为按钮的click事件指定行索引。

像这样==>

You could just put the code that you want to execute in a method of its own and then call this one from both event handlers. Of course you will have to specify the row index yourself in the event handler for the click event of the button though.
Like this ==>
private void DoMyFunction(int rowIndex)
        {



//在这里做你的计算好吧:))}






//Do your calculation in here ok:))}


private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5 || e.ColumnIndex == 6)
            {
                DoWork(e.RowIndex);
            }
        }







private void button3_Click(object sender, EventArgs e)
     {
         int row = 0; //always specify the row here...
         DoMyFunction(row);
     }





希望有帮助但是......



Hope that helps though...


当然,你可以参考你的gridview对象:



GridView1.Rows [row_number] .Cells [cell_number] .Value = your_calculations。



看一下DataGridView类的方法和属性。



DataGridView Class
Of course, you can refer to your gridview object:

GridView1.Rows[row_number].Cells[cell_number].Value = your_calculations.

Take a look at DataGridView class methods and properties.

DataGridView Class


亲爱的朋友们......



使用此...



private void btnChoose_Click(object sender,EventArgs e){



MouseEventArgs b = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left,2,

MousePosition.X,MousePosition.Y,0);

DataGridViewCellMouseEventArgs a = new DataGridViewCellMouseEventArgs(0,0,

MousePosition.X,MousePosition.Y,b);

dataGridView1_CellMouseDoubleClick(sender,a) ;

}



享受它......
Hi to all my dear friends...

use this...

private void btnChoose_Click(object sender, EventArgs e) {

MouseEventArgs b = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 2,
MousePosition.X, MousePosition.Y, 0);
DataGridViewCellMouseEventArgs a = new DataGridViewCellMouseEventArgs(0, 0,
MousePosition.X, MousePosition.Y, b);
dataGridView1_CellMouseDoubleClick(sender, a);
}

enjoy it...


这篇关于如何在Button_Click中调用Datagridview Cell Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆