Invalid Cast Exception using Inherited Object

C

cgoetz

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.
 
P

Phill W.

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.
 
C

cgoetz

Phill said:
... which is extracting a reference to a DataGridTextBoxColumn from
this class ...


... 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.
 
C

Chris Dunaway

cgoetz said:
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))
 
P

Phill W.

cgoetz said:
.. . .
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.
 
C

cgoetz

Thanks to both of you for taking the time to look into this. Looks
like its back to the drawing board. Thanks again.
 

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