Invalid Cast Errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan

    Invalid Cast Errors

    I keep getting an invalid cast error on this line of code:

    Dim gridCol As MyGridColumn = CType(dgt.GridC olumnStyles(hi. Column),
    MyGridColumn)

    I am trying to develop a sub that will change the colors on a cell that
    is clicked on in a DataGrid control.

    Here is the full code:

    Private Sub PaintCell(ByVal sender As Object, ByVal e As
    MouseEventArgs)

    Try
    Dim hi As DataGrid.HitTes tInfo
    Dim grid As DataGrid = CType(sender, DataGrid)
    hi = grid.HitTest(e. X, e.Y)
    If hi.Type = DataGrid.HitTes tType.Cell Then
    Dim dgt As DataGridTableSt yle =
    dgvBranches.Tab leStyles(0)
    Dim cm As CurrencyManager =
    CType(Me.Bindin gContext(myData Set.Tables(0)), CurrencyManager )
    Dim cellRect As Rectangle
    cellRect = grid.GetCellBou nds(hi.Row, hi.Column)
    Dim gridCol As MyGridColumn =
    CType(dgt.GridC olumnStyles(hi. Column), MyGridColumn)
    Dim g As Graphics = dgvBranches.Cre ateGraphics()
    Dim fBrush As New SolidBrush(Colo r.Blue)
    Dim bBrush As New SolidBrush(Colo r.Yellow)
    gridCol.PaintCo l(g, cellRect, cm, hi.Row, bBrush,
    fBrush, False)
    End If
    Catch ex As Exception
    LogErrors(ex, "PaintCell" )
    End Try
    End Sub 'PaintCell


    Any help would be appreciated. Thanks!

  • Chris Dunaway

    #2
    Re: Invalid Cast Errors

    What data type is MyGridColumn? Does it inherit from another class?
    Is the class it inherits from a DataGridColumnS tyle? The
    dgt.GridColumnS tyles collection is a collection of DataGridColumnS tyle
    so if your gridCol is not a DataGridColumnS tyle or a class that
    inherits from it, the cast wont work.

    Also, I noticed that your sub create a number of objects that need to
    be disposed, namely the Graphics object and the Brush objects. You
    should call .Dispose on these objects to free up their unmanaged
    resources.

    Comment

    • Alan

      #3
      Re: Invalid Cast Errors

      Sorry I knew there was something I forgot to add to this:

      Public Class MyGridColumn
      Inherits DataGridTextBox Column
      Public Sub PaintCol(ByVal g As Graphics, ByVal cellRect As
      Rectangle, _
      ByVal cm As CurrencyManager , ByVal rowNum As Integer, ByVal bBrush
      As Brush, _
      ByVal fBrush As Brush, ByVal isVisible As Boolean)
      Me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
      End Sub
      End Class

      Perhaps that will answer your questions Chris.

      Comment

      • Alan

        #4
        Re: Invalid Cast Errors

        Needless to say I still get the error.

        Comment

        • Chris Dunaway

          #5
          Re: Invalid Cast Errors

          Are the items in dgt.GridColumnS tyles in fact instances of
          MyGridColumn? Can you verify using the debugger what type
          dgt.GridColumnS tyles(hi.Column ) is? If it is a MyGridColumn instance
          then I don't know why the cast is not working.

          If if is an instance of one of the base classes (DataGridTextBo xColumn
          or DataGridColumnS tyle) then the cast won't work because you can't cast
          from a base type to a derived type unless the object is, in fact, an
          instance of the derived type.

          PS. I still stand by recommendation to Dispose your brushes and
          Graphics instances.

          Comment

          Working...