473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Invalid Cast Exception using Inherited Object

Hi all,

I'm using VS 2003 with the 1.1 framework and am experimenting with the
DataGrid and am applying several different techniques to it. Here is
what works:

Dim WithEvents datagridtextBox As DataGridTextBoxColumn
Apply different style criteria to the DataGrid and then...
datagridtextBox = CType(Me.TableStyles(0).GridColumnStyles(1),
DataGridTextBoxColumn)

With the above code everything works like it should. I have a class
that inherits from DataGridTextBoxColumn called MultiLineColumn. I am
using it in other stand alone grids to word wrap the text in some
columns and also dynamically size the height of the rows. In this
particular application, when I do this:

Dim WithEvents multiLineTextColumn As MultiLineColumn
Apply different style criteria to the DataGrid and then...
multiLineTextColumn = CType(Me.TableStyles(0).GridColumnStyles(0),
MultiLineColumn)

Not good. I get an error when running the app stating that the
specified cast is not valid. What could the problem be? Have been
playing with this for a while, so I could have easily missed something
obvious. Any help in the right direction would be much appreciated.

Mar 1 '06 #1
5 2012

"cgoetz" <cg****@gpoga.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Dim WithEvents datagridtextBox As DataGridTextBoxColumn
Apply different style criteria to the DataGrid and then...
datagridtextBox = CType(Me.TableStyles(0).GridColumnStyles(1),
DataGridTextBoxColumn)
.... which is extracting a reference to a DataGridTextBoxColumn from
this class ...
Dim WithEvents multiLineTextColumn As MultiLineColumn
Apply different style criteria to the DataGrid and then...
multiLineTextColumn = CType(Me.TableStyles(0).GridColumnStyles(0),
MultiLineColumn)


.... which is /trying/ to do the same thing for your derived class.

Just /how/ does your MultiLineColumn object get added to the grid?

You can always test the type of an object /before/ you cast it, as in

Dim gcs0As ... _
Me.TableStyles(0).GridColumnStyles(0)

If TypeOf gcs0 Is MultiLineColumn Then
multiLineTextColumn = CType( gcs0, MultiLineColumn)
ElseIf TypeOf gcs0 Is DataGridTextBoxColumn Then
...
End If

HTH,
Phill W.
Mar 1 '06 #2

Phill W. wrote:
"cgoetz" <cg****@gpoga.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Dim WithEvents datagridtextBox As DataGridTextBoxColumn
Apply different style criteria to the DataGrid and then...
datagridtextBox = CType(Me.TableStyles(0).GridColumnStyles(1),
DataGridTextBoxColumn)


... which is extracting a reference to a DataGridTextBoxColumn from
this class ...
Dim WithEvents multiLineTextColumn As MultiLineColumn
Apply different style criteria to the DataGrid and then...
multiLineTextColumn = CType(Me.TableStyles(0).GridColumnStyles(0),
MultiLineColumn)


... which is /trying/ to do the same thing for your derived class.

Just /how/ does your MultiLineColumn object get added to the grid?

You can always test the type of an object /before/ you cast it, as in

Dim gcs0As ... _
Me.TableStyles(0).GridColumnStyles(0)

If TypeOf gcs0 Is MultiLineColumn Then
multiLineTextColumn = CType( gcs0, MultiLineColumn)
ElseIf TypeOf gcs0 Is DataGridTextBoxColumn Then
...
End If

HTH,
Phill W.


After setting the datasource for the grid, I use this method to read
the contents of grid and then dynamically set the type of control to
display.

dtp = New DateTimePicker
dtp.Dock = DockStyle.Fill
dtp.Cursor = Cursors.Arrow

If hitTestGrid.Row >= 0 AndAlso hitTestGrid.Row <= bmb.Count Then
If Me(hitTestGrid.Row, 2).ToString().Equals("DateTime")
Then
Dim value As DateTime =
DateTime.Parse(Me(hitTestGrid.Row, 1).ToString())
datagridtextBox.TextBox.Controls.Add(dtp)
dtp.Value = value
EndIf
EndIf

Since I'm filling the grid with data and then applying the style, the
columns are of the default type. I iterate through the collection of
columns to set the width, alignment, etc. Basically, I'm setting my
object equal to the first column of the grid, using the tablestyle then
setting up event handlers for when the columns will gain focus.

Mar 1 '06 #3
cgoetz wrote:
Dim WithEvents multiLineTextColumn As MultiLineColumn
Apply different style criteria to the DataGrid and then...
multiLineTextColumn = CType(Me.TableStyles(0).GridColumnStyles(0),
MultiLineColumn)


What type is Me.TableStyles(0).GridColumnStyles(0) ?? Is it in fact a
MultiLineColumn? Or is it a DataGridTextBoxColumn? If the latter is
true, then you cannot cast it. You cannot cast a base type as a
derived type.

What you might do is create an additional constructor in your
MultiLineColumn class that accepts a DataGridTextBoxColumn and use that
to create the MultiLineColumn instance. Something like this:

Dim WithEvents multiLineTextColumn As MultiLineColumn
multiLineTextColumn = New
MultiLineColumn(Me.TableStyles(0).GridColumnStyles (0))

Mar 1 '06 #4

"cgoetz" <cg****@gpoga.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...

Phill W. wrote:
"cgoetz" <cg****@gpoga.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
.. . . Just /how/ does your MultiLineColumn object get added to the grid?
.. . . Since I'm filling the grid with data and then applying the style, the
columns are of the default type.


And therein lies your problem - all the columns are of type
DataGridTextBoxColumn (the base type) and /not/ of your
carefully-crafted, derived class. Because of this, you /cannot/
cast one of the grid columns into /your/ class, because it /isn't/ one.

? TypeOf Me.TableStyles(0).GridColumnStyles(1) Is DataGridTextBoxColumn
True

? TypeOf Me.TableStyles(0).GridColumnStyles(1) Is MultiLineColumn
False

You have to find a way to add /your/ [class of] "column" object(s) into
the grid /instead/ of the default ones.

HTH,
Phill W.
Mar 2 '06 #5
Thanks to both of you for taking the time to look into this. Looks
like its back to the drawing board. Thanks again.

Mar 2 '06 #6

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

Similar topics

1
by: Steph | last post by:
Hello, I'm using COM object in an application. One method used give an object wich type is object. When I use a GetType on its, I have "System.String". I try to convert it to loop on the...
0
by: Alan Z. Scharf | last post by:
Win Server 2003 VS.Net 2003 --------------- 1. I'm having the same problem below on all six of my pages with a datagrid item. 2. These pages all worked fine for months until problem started....
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
3
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....
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
21
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
7
by: Chris Thunell | last post by:
I'm trying to loop through an exchange public folder contact list, get some information out of each item, and then put it into a vb.net datatable. I run though the code and all works fine until i...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.