6 Replies - 994 Views - Last Post: 20 January 2012 - 02:13 PM Rate Topic: -----

#1 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 11:20 AM

I have a loop that will determine if a certain value is within the specification limits that I assign. The problem is, when the loop starts, I get the following exception:

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

Is This A Good Question/Topic? 0
  • +

Replies To: InvalidOperationException on trying to color DGV cell

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 11:33 AM

What about this part?
Dim spec As DataTable = GetTestSpec(cell.OwningColumn.Name, Integer.Parse(cbxSpec.SelectedItem))


Code for this function? Is it just using this as a reference guide for comparison? Breakpoint, where does the error come form, what line - check your variables while in pause?

This post has been edited by _HAWK_: 20 January 2012 - 11:34 AM

Was This Post Helpful? 0
  • +
  • -

#3 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 11:37 AM

That code just retrieves the maximum and minimum spec values from the database. The line that causes the problem is line 04 (before it ever even hits that particular snippet of code).
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 12:13 PM

Just to keep it straight.

You said: The exception occurs as soon as I call ColorAllCells

Where now you are saying the error is on: ColorCells(dgvCurSetLanes.Rows(row).Cells(col))

So it errors on this call ColorCells on line 4, but does not enter the sub?
Was This Post Helpful? 0
  • +
  • -

#5 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 12:37 PM

View Post_HAWK_, on 20 January 2012 - 12:13 PM, said:

Just to keep it straight.

You said: The exception occurs as soon as I call ColorAllCells

Where now you are saying the error is on: ColorCells(dgvCurSetLanes.Rows(row).Cells(col))

So it errors on this call ColorCells on line 4, but does not enter the sub?


Correct.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 01:13 PM

I think it would be preferable to make a function that returns the color by passing the values of the cell to it.
Was This Post Helpful? 1
  • +
  • -

#7 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: InvalidOperationException on trying to color DGV cell

Posted 20 January 2012 - 02:13 PM

I'll give it a try, thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1