472,342 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Specified cast not valid

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),
' 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,
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

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
Nov 20 '05 #1
3 1616
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?

Nov 20 '05 #2
Thank you for responding,

In variable:


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!
"Fergus Cooney" <fi******@tesco.net> wrote in message news:<uu**************@TK2MSFTNGP12.phx.gbl>...
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?


Nov 20 '05 #3
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

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

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.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Tao | last post by:
I just upgraded .NET framework to 1.1 and VS.Net to 2003 version and tried to test it out. I created an ASP.NET project using the wizard and tried...
by: Tyro | last post by:
Can someone shed some light on my error here? Thanks! Specified cast is not valid. Exception Details: System.InvalidCastException: Specified...
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my...
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I...
by: VB Programmer | last post by:
I am setting up forms authentication. In my code I keep getting this error. Any ideas? Error.... Server Error in '/LandOLots'...
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005...
by: Alan Z. Scharf | last post by:
this question in datagrid group for several days with no repsonse. I'm hoping for an answer her because of greater activity in this group. No...
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception...
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/...
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { ...
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.