Stack Exchange
Automatic Execute When the Form is loaded
Please help me this.
I have the following code (C#):
        decimal quantity = 0, avai = 0, newavai = 0;
        decimal totalstock = 0, newtotalstock = 0;

        if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
        {
            newavai = avai + quantity;
            G1.Rows[e.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
        }
        if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity))
        {
            newtotalstock = totalstock + quantity;
            G1.Rows[e.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
        }

This code is just about calculating the cell in Datagridview and it worked perfectly fine. Thing is, I want that automatic execute when the form is loaded. Now I just temporary put it in Cell Click event because I wanna test the code and dont know where to put the code, please help me. 
Thank you.

Why not put it in Form_Load event handler?

@WillyDavidJr you cannot call e.RowIndex in Form_Load because it relevant to datagridview thing

You can if you do your databinding in form load.   Of course there will be no e.  Perhaps you need to tell us more what you are trying to do?

@MickyD like a title, I want that when I load the form, this code will automatic execute. That all I need

Then just do as what Willy and I said.  Actually @Willy has a fine answer below :)

You can do that with two options:
First create a new instance of DataGridViewCellEventArgs then specify the column index and row index, you can refer to this link for more info:
 DataGridViewCellEventArgs
Then I replace all your e with ee since in Form Load, variable e is already been used. That's it.
    private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(1, 1);

        decimal quantity = 0, avai = 0, newavai = 0;
        decimal totalstock = 0, newtotalstock = 0;

        if (decimal.TryParse(G1.Rows[ee.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[ee.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
        {
            newavai = avai + quantity;
            G1.Rows[ee.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
        }
        if (decimal.TryParse(G1.Rows[ee.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[ee.RowIndex].Cells["Quantity"].Value.ToString(), out quantity))
        {
            newtotalstock = totalstock + quantity;
            G1.Rows[ee.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
        }
    }

The other option is that you can call the click event handler with this:
 private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(1, 1);
        G1_CellClick(sender, ee);
    }

private void G1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
       decimal quantity = 0, avai = 0, newavai = 0;
        decimal totalstock = 0, newtotalstock = 0;

        if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["AvailableStock"].Value.ToString(), out avai))
        {
            newavai = avai + quantity;
            G1.Rows[e.RowIndex].Cells["AvailableStock"].Value = newavai.ToString();
        }
        if (decimal.TryParse(G1.Rows[e.RowIndex].Cells["TotalStock"].Value.ToString(), out totalstock) && decimal.TryParse(G1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString(), out quantity))
        {
            newtotalstock = totalstock + quantity;
            G1.Rows[e.RowIndex].Cells["TotalStock"].Value = newtotalstock.ToString();
        }
    }


Sorry for dumb question, why I create DataGridViewCellEventArgs, the following error show up: Method must have a return type

Since this code is under dataGridView1_CellClick event handler right? You said in your post that this is under Cell Click event handler which requires DataGridViewCellEventArgs unless I am missing something. @Serenade

Yes the code is under G1_CellClick (name of the dgv :D )
you tell me that create a new instance of DataGridViewCellEventArgs in your following link.
public DataGridViewCellEventArgs(  int columnIndex,  int rowIndex )
I put this code in my project but got error

What is the error? Just this code will do: DataGridViewCellEventArgs ee = new DataGridViewCellEventArgs(1, 1); No need to add public, just specify the column index and row index. In this case, 1,1 is just my example. It will still depend on what row and index you are trying to debug.

aww I got it, let me try again

Good to hear that, I updated my code, I added another option, you can try that as well.

hey there are anywhere else I can discuss with you because the site will stop me to extended discussion in a few comments next.

Kindly join my chat room instead: http://chat.stackoverflow.com/rooms/143232/david

joined, but sadly cannot chat because it needs 20 rep to do it

Me too, I just voted it up. Unfortunately you need 2 more points.

yeah 2 more point,
and your other option: when I try to put ee.rowindex, the error showed up that ee is not define yet

Sorry I updated my code, kindly check again the updated code on second option. Btw, you already have 23 points, you can now chat with me :) @Serenade

Try chat again.. :)

@WillyDavidJr dear, thank you, I have 23 points now but why it still said that I need 20 point to chat, do I need to wait for the admin confirm or something?
because I really need to chat with you riight now

The points is not updated on my part as well. There is a delay in points update I think. @Serenade

@WillyDavidJr then wait a little bit, I will get in touch with you really soon

Can I ask you this? I just applied your code but I'm not see any different, did I do anything wrong?

@WillyDavidJr dear, now I can chat, please invite me again :)

dear, I already joined, sorry for delay, just went out for lunch :)
