mike Delvotti

mike Delvotti

  • NA
  • 262
  • 0

Check if datagridview check box is checked

Mar 19 2015 8:24 AM
Hi Guys,

I have a datagidview where the first column is a checkbox column.

Then a user can check selected rows, click a button and delete those selected rows, this bit works fine.

My problem is, that it throws an error if no check boxes are checked. How can I edit my code below to loop through datagrid first and throw message if no boxes are checked?

My code below:

DialogResult yourAnswer = MessageBox.Show("Are you sure you want to delete the selected items?", "Caption",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (yourAnswer == DialogResult.Yes)
            {
                for (int i = supplyDataGridView.Rows.Count - 1; i >= 0; i--)
                {
                    object temp = supplyDataGridView.Rows[i].Cells[0].Value;
                    if (!Convert.IsDBNull(temp) && Convert.ToBoolean(temp))
                    {
                        supplyDataGridView.Rows.RemoveAt(i);
                    }
                }
              }
            else
            {

            }


Answers (11)