C# 我的代码第一次不执行?为什么?

C# 我的代码第一次不执行?为什么?,c#,C#,我在我的项目中有这样的代码,我在这个网站的帮助下完成了它,我的代码检查datagridviewcheckboxcolumn的检查状态,然后它检查datagridview的另一个单元格,用这些检查执行一个方法,我的所有代码都在一个按钮点击内。 这是我的密码 private void update_bt_Click(object sender, EventArgs e) { var current_year = DateTime.Today.Year; var last_year =

我在我的项目中有这样的代码,我在这个网站的帮助下完成了它,我的代码检查datagridviewcheckboxcolumn的检查状态,然后它检查datagridview的另一个单元格,用这些检查执行一个方法,我的所有代码都在一个按钮点击内。 这是我的密码

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);
            }
        }
    }
}
private void update\u bt\u单击(对象发送者,事件参数e)
{
var current_year=DateTime.Today.year;
var last_year=DateTime.Today.year-1;
对于(int i=0;i
{
int.TryParse(row.Cells[“stg_id”].Value.ToString(),out j);
返回j;
};
DataGridViewCheckBoxCell chkchecking=dgv_student_update.Rows[i]。单元格[“result”]作为DataGridViewCheckBoxCell;
if(Convert.ToBoolean(chkchecking.Value)==true)
{
//检查每行中的单元格[3]是否等于阶段id(1=第一阶段)。
如果(单元格值(dgv_student_update.Rows[i],3)==1)
{
sc.学年\学生\更新(
转换.ToInt32(dgv_student_update.Rows[i].Cells[1].Value),
dgv_student_update.Rows[i].Cells[2].Value.ToString(),2);
}
}
}
}

当我点击按钮时,它不会执行“year\u student\u update()”方法,但在第二次点击时,它会执行任务。任何人都可以帮我吗?

你的方法
sc.year\u student\u update(…
if
条件
if(cellValue(dgv\u student\u update.Rows[i],3)==1)内
这很可能不是
正确的
。请注释
if
阻塞并查看您的方法是否成功。如果成功,那么您应该检查
if
阻塞不令人满意的原因。

您使用的是Visual Studio?WPF?WinForms?ASPX?是Visual Studio 2013。WinForms这里定义了“sc”吗?sc是另一个类的对象“year_student_update()”方法在该类中,但我确定问题不在该类中。例如,当我将一个messageBox放在if中以代替该方法时,它会首先单击给我消息,这意味着两个if为真,if为真。