Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?
C#
void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
   //Do something
}button1_click(object sender Args e)
{
//how do I call gridview method in here??
}
Posted
Updated 16-Oct-18 9:46am
v4

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 ==>
C#
private void DoMyFunction(int rowIndex)
        {

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


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


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


Hope that helps though...
 
Share this answer
 
v2
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
 
Share this answer
 
Comments
Richard Deeming 23-Mar-15 12:02pm    
You just need to create an instance of the DataGridViewCellEventArgs class and pass it to the method:

var args = new DataGridViewCellEventArgs(theColumnIndex, theRowIndex);
dataGridView2_CellContentClick(dataGridView2, args);
Julia Fraveau 23-Mar-15 12:12pm    
Thanks Richard, I have copied ur code, var args = new DataGridViewCellEventArgs(theColumnIndex, theRowIndex);
dataGridView2_CellContentClick(dataGridView2, args);
but I get its yelling at me about the parameters do I need to declare those theColumnIndex and theRowIndex first?
Richard Deeming 23-Mar-15 14:58pm    
You would need to replace theColumnIndex and theRowIndex with the column and row indices that you wanted to process.
Julia Fraveau 24-Mar-15 7:51am    
Thanks again for your time and guide Richard, I have done like this var args = new DataGridViewCellEventArgs(ColumnIndex, RowIndex);
dataGridView2_CellContentClick(dataGridView2, args);ColumnIndex and RowIndex are the two indices here but am still getting errorrs, and when ever I hover around DataGridViewCellEventArgs I see (int columnIndex, int rowIndex)I have no idea why still ,getting errors
Richard Deeming 24-Mar-15 8:02am    
What errors are you getting?
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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900