0

My DataGridView is set up as a table with 3 columns, [INSPECTED]Checkbox, [SHIPPED]Checkbox, and [SERIAL #], which isn't relevant to this question. I have it set up so in order to have SHIPPED checked off, INSPECTED must be checked of as well, and if both were originally checked off, then inspected is checked off, shipped is also checked off.

INSPECTED | SHIPPED | SERIAL #


private void table_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
            foreach (DataGridViewRow row in table.Rows)
            {
                var inspect = (DataGridViewCheckBoxCell)row.Cells["INSPECTED"];
                var ship = (DataGridViewCheckBoxCell)row.Cells["SHIPPED"];
                
                if(inspect.Selected == true)
                {
                    if ((bool)inspect.Value == false) 
                    {
                        inspect.Value = true;
                    }
                    else
                    {
                        inspect.Value = false;
                        if((bool)ship.Value == true)
                        {
                            MessageBox.Show("SHIPPED UNTICKED");
                        }
                        ship.Value = false;
                    }
                }
                if(ship.Selected == true)
                {
                    bool test = false;
                    if((bool)inspect.Value == true)
                    {
                        ship.Value = true;
                    }
                    else
                    {
                        MessageBox.Show("MUST BE INSPECTED FIRST");
                        ship.Value = false; 
                        table.Refresh();
                    }
                }
            }
}

When running through the loop after checking off the shipping box but NOT the inspected box, it hits the messagebox that states "MUST BE INSPECTED FIRST", which should mean it runs ship.value = false, but the table doesn't show the checkbox as unticked. And when printing out ship.value to console, it returns false.

  • Call `table.RefreshEdit();` to reflect the changes. Side, showing a modal dialog in a loop is bad idea. Use the [DataGridViewCell.ErrorText](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewcell.errortext?view=windowsdesktop-7.0) property instead to mark the invalid cell values and clear the property for the valid ones. You could show a message box after the loop. – dr.null Aug 17 '23 at 22:26

0 Answers0