my code does not execute the at first time? why?

时间:2016-07-11 19:09:14

标签: c#

i have this code in my project that i do it with a help in this web site, my code check the datagridviewcheckboxcolumn checked state after that it checks another cell of datagridview with these to checks it execute a method, all my code is inside a button click. this is my code

private void update_bt_Click(object sender, EventArgs e)
{
    var current_year = DateTime.Today.Year;
    var last_year = DateTime.Today.Year - 1;
    for (int i = 0; i < dgv_student_update.Rows.Count; i++)
    {
        Func<DataGridViewRow, int, int> cellValue = (row, j) =>
        {
            int.TryParse(row.Cells["stg_id"].Value.ToString(), out j);
            return j;
        };
        DataGridViewCheckBoxCell chkchecking = dgv_student_update.Rows[i].Cells["result"] as DataGridViewCheckBoxCell;
        if (Convert.ToBoolean(chkchecking.Value) == true)
        {
            //check if cell[3] in each row equal to stage id (1=first stage).
            if (cellValue(dgv_student_update.Rows[i], 3) == 1)
            {
                sc.year_student_update(
                Convert.ToInt32(dgv_student_update.Rows[i].Cells[1].Value),
                dgv_student_update.Rows[i].Cells[2].Value.ToString(), 2);
            }
        }
    }
}

when i click button it does not execute the "year_student_update()" method but at the second click it do the job. anyone can help me please?

1 个答案:

答案 0 :(得分:3)

Your method sc.year_student_update(... is inside the if condition if (cellValue(dgv_student_update.Rows[i], 3) == 1) and most probably that's not true. comment that if block and see if your method hits. If so, then you should check why your if block not satisfying.