C# 如何更改此代码以仅接收一个msgbox?

C# 如何更改此代码以仅接收一个msgbox?,c#,checkbox,datagridview,C#,Checkbox,Datagridview,我有带有复选框的datagridview列。我想检查此列中是否有选定行。如果不是,我想显示msgbox。我可以通过这个来检查列,但是对于每个未检查的行,我会收到多个MsgBox。如何将其更改为只接收一个msgbox foreach (DataGridViewRow row in dataGridView1.Rows) { DataGridViewCheckBoxCell cell = row.Cells[5] as DataGridViewCheckBoxCell; //We

我有带有复选框的datagridview列。我想检查此列中是否有选定行。如果不是,我想显示msgbox。我可以通过这个来检查列,但是对于每个未检查的行,我会收到多个MsgBox。如何将其更改为只接收一个msgbox

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell cell = row.Cells[5] as DataGridViewCheckBoxCell;

    //We don't want a null exception!
    if (cell.Value == null)
    {
        MessageBox.Show("Nie zaznaczono pola!");
    }
}
编辑:

谢谢你的回答。我意识到我的问题比我想象的更复杂。我的按钮必须从datagrid中删除选定的行。问题在于foreach,因为它只删除第一行,在使用if/else时仍然显示msgbox。我需要删除数据,如果未选中复选框,请执行msgbox。这是我的按钮:

private void button2_Click(object sender, EventArgs e)
{
    con.Open();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        object cell = row.Cells["cell_del"].Value;
        if (cell == "yes")
        {

            SqlCommand cmd = new SqlCommand("Delete From Rok2016 where ID ='" + row.Cells[4].Value.ToString() + "'", con);
            cmd.ExecuteNonQuery();

            wczytywanie_tabeli();
        }

        else 
        {                               
            MessageBox.Show("Nie zaznaczono pola!");
            break;
        }
    }




    con.Close();
}

如果找到空值,只需断开循环即可在获得空值后停止循环:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell cell = row.Cells[5] as DataGridViewCheckBoxCell;

    //We don't want a null exception!
    if (cell.Value == null)
    {
        MessageBox.Show("Nie zaznaczono pola!");
        break;
    }
}