Specified cast not valid

M

Mike Cooper

Hello All!

I am getting teh above error message on the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim dgt As DataGridTableStyle = Main_DataGrid.TableStyles(0)

Dim cm As CurrencyManager = CType(Me.BindingContext _
(Main_DataSet.Tables(dgt.MappingName)), CurrencyManager)
' Get the Rectangle of the clicked cell.
Dim cellRect As Rectangle
Dim cellrow As Integer = 5
Dim cellcolumn As Integer = 1
cellRect = Main_DataGrid.GetCellBounds(5, 1)
' Get the clicked DataGridTextBoxColumn.

Dim gridCol As New MyGridColumn()
gridCol = CType(dgt.GridColumnStyles(cellcolumn),
MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = Main_DataGrid.CreateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Color.Yellow)
Dim bBrush As New SolidBrush(Color.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, cellrow, bBrush, fBrush,
False)


My MyGridColumn class looks like this:

Public Class MyGridColumn
Inherits DataGridTextBoxColumn
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


Some of you may recognize this; it is only a slight revision of an
exam given by Microsoft at MSDN. I thought I would be safe using that
code. I get the error at "gridcol = CTYPE" statment. Everything looks
right to me! I have a valid GridTableStyles[0] and a
GridColumnStyles[1].

My only thought: I can't seem to set a variable to a 'type' of
GridColumnStyles. Can anyone tell me if it is correct to reference a
GridColumnstyle directly in this way. ALL SUGGESTIONS WOULD BE
APPRECIATED!!!
 
F

Fergus Cooney

Hi Mike,

I'm not entirely sure what you're doing but ..

dgt.GridColumnStyles is a collection of DataGridColumnStyle objects and
these cannot be referenced by a MyGridColumn object.

Are you sure that you want something from dgt.GridColumnStyles and not an
actual column?

Regards,
Fergus
 
M

Mike Cooper

Thank you for responding,

In variable:

dgt.GridColumnStyles(cellcolumn)

dgt is my datagridtablestyle and cellcolumn is an integer = 1. This
is actually
a reference then for dataGridColumnStyle(0) which is the
DataGridTextBoxColumn column I am after. Just in
case this was the problem however, I change this reference to the
actual of DataGridTextBoxColumn2 the result is the same.

Have you, or anyone else out there attempted to use CTYPE to convert a
parent class object into a child object (which inherited the parent)?
I don't see why it shouldn't work!
 
F

Fergus Cooney

Hi Mike,

My apologies, I took DataGridTextBoxColumn at its word and didn't look it
up. Given that it inherits from DataGridColumnStyle I would have thought it
would be called DataGridTextBoxColumnStyle. It fooled me!.

Anyway, I can answer your question now that I see the problem.

If dgt.GridColumnStyles(cellcolumn) contains a MyGridColumn (style) then
you can cast it. If it contains a descendant (child or further) of
MyGridColumn then you would be also able to cast it.

But not an <ancestor> (parent or further).

Let's get away from Data objects and just go to objects and references.

An Object variable can reference anything but it will only give you access
to the things that the Object class provides. Let's say it referenced a
String. To get the String methods you would have to cast it to a string first
(if Option Strict is on), or it would be obtained at runtime through late
binding.

Dim O As Object = "Some text"
Dim I As Integer = O.Length 'Fails to compile (OS on), or late binds.
Dim I As Integer = DirectCast (O, String).Length 'No problems.

But the converse is not possible. There's no way that a String reference
can reference an Object.

Dim O As Object = "Some text"
Dim S As String = O 'Fails to compile (OS on)
'or fine at run-time because O references a
String.

Dim O As New Object
Dim S As String = O 'Fails to compile (OS on)
'or gives Invalid Cast at run-time.

This is because a String reference is expecting to be able to access <any
and all> of the methods of Strings. Object, of course, doesn't provide any of
the extra facilities that Strings have. Hence - Bang!

Back to your problem, then.

I expect that dgt.GridColumnStyles(cellcolumn) is an actual
DataGridTextBoxColumn. And you're trying to cast it to a derivative,
MyGridColumn, with its extra facilities. But, as, DataGridTextBoxColumn
doesn't have them - Bang!

That's the problem. There are two types of solution.

One is to keep the derived class and ensure that the dgt.GridColumnStyles
collection is composed of MyGridColumns, either entirely, or just with those
that'll have a PaintCol requirement.

The other solution is to abandon the derived class. Then you might pass
gridCol to ColPaint as a parameter.

Regards,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top