473,714 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Specified cast not valid

Hello All!

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

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click

Dim dgt As DataGridTableSt yle = Main_DataGrid.T ableStyles(0)

Dim cm As CurrencyManager = CType(Me.Bindin gContext _
(Main_DataSet.T ables(dgt.Mappi ngName)), 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.G etCellBounds(5, 1)
' Get the clicked DataGridTextBox Column.

Dim gridCol As New MyGridColumn()
gridCol = CType(dgt.GridC olumnStyles(cel lcolumn),
' Get the Graphics object for the form.
Dim g As Graphics = Main_DataGrid.C reateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Colo r.Yellow)
Dim bBrush As New SolidBrush(Colo r.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCo l(g, cellRect, cm, cellrow, bBrush, fBrush,
My MyGridColumn class looks like 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
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
GridColumnStyle s[1].

My only thought: I can't seem to set a variable to a 'type' of
GridColumnStyle s. 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 1695
Hi Mike,

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

dgt.GridColumnS tyles is a collection of DataGridColumnS tyle objects and
these cannot be referenced by a MyGridColumn object.

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

Nov 20 '05 #2
Thank you for responding,

In variable:

dgt.GridColumnS tyles(cellcolum n)

dgt is my datagridtablest yle and cellcolumn is an integer = 1. This
is actually
a reference then for dataGridColumnS tyle(0) which is the
DataGridTextBox Column column I am after. Just in
case this was the problem however, I change this reference to the
actual of DataGridTextBox Column2 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******* *******@TK2MSFT NGP12.phx.gbl>. ..
Hi Mike,

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

dgt.GridColumnS tyles is a collection of DataGridColumnS tyle objects and
these cannot be referenced by a MyGridColumn object.

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


Nov 20 '05 #3
Hi Mike,

My apologies, I took DataGridTextBox Column at its word and didn't look it
up. Given that it inherits from DataGridColumnS tyle I would have thought it
would be called DataGridTextBox ColumnStyle. It fooled me!.

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

If dgt.GridColumnS tyles(cellcolum n) 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.GridColumnS tyles(cellcolum n) is an actual
DataGridTextBox Column. And you're trying to cast it to a derivative,
MyGridColumn, with its extra facilities. But, as, DataGridTextBox Column
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.GridColumnS tyles
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 to run it by hitting "F5". I got an exception: "Specified cast is not valid." The only thing i put there was a "test this." inside the form. What might be the problem here? Thanks in advance. The Exception:
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 cast is not valid. Source Error: Stack Trace:
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 page. I am trying to populate a datagrid. One of my columns is a template column where I'd like to evaluate the data brought back from the db and populate the column with a "Y" or "N" depending on the value in the db. Here is the code that is causing it as well as the error (below). ERROR...
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 get data form a database. The code snipped: try {
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' Application. --------------------------------------------------------------------------------
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 10:50:14 AM,580,AUE1\Administrator,"Specified cast is not valid.","Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> Microsoft.BusinessIntelligence.Scorecard.ScorecardException: Specified cast is not valid. ---> System.InvalidCastException: Specified...
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 cross-posting intended. Thanks. ----------------------------------------------------------- Server VS.NET 2003 SQLServer 2000 IIS 66.0
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 "System.InvalidCastException" ("Specified cast is not valid"). How do I fix it ? using System.Diagnostics; .. .. class NewProcess: Process {
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/ the keys involved. The stack trace is: ====================== Message: Specified cast is not valid. Type: System.InvalidCastException Source: System.Data.Linq TargetSite: Boolean TryCreateKeyFromValues(System.Object, V ByRef)
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; Microsoft.Web.UI.WebControls.TreeNode iNode = null;
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.