System.InvalidOperationException was unhandled
Message=Cell provided already belongs to a grid. This operation is not valid.
I'm not sure exactly why I am getting this exception...OF COURSE the cell is already part of a grid! I just want to color it! Anyway, here is the code I am using:
Private Sub ColorAllCells() For row As Integer = 0 to dgvCurSetLanes.Rows.Count - 1 For col As Integer = 0 to dgvCurSetLanes.Columns.Count - 1 ColorCells(dgvCurSetLanes.Rows(row).Cells(col)) Next Next End Sub Private Sub ColorCells(ByRef cell As DataGridViewCell) Dim spec As DataTable = GetTestSpec(cell.OwningColumn.Name, Integer.Parse(cbxSpec.SelectedItem)) If spec Is Nothing Then Exit Sub If ValueInSpec(cell.Value, spec.Rows(0).Item("min"), spec.Rows(0).Item("max")) = True Then cell.Style.BackColor = Color.LightGreen Else 'not in spec cell.Style.BackColor = Color.LightSalmon End If End Sub
The exception occurs as soon as I call ColorAllCells