Home All Groups Group Topic Archive Search About

Invalid Cast Exception using Inherited Object

Author
1 Mar 2006 3:40 AM
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.

Author
1 Mar 2006 9:38 AM
Phill W.
"cgoetz" <cgo***@gpoga.com> wrote in message
news:1141184438.939172.243480@u72g2000cwu.googlegroups.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.
Author
1 Mar 2006 12:15 PM
cgoetz
Phill  W. wrote:
Show quoteHide quote
> "cgoetz" <cgo***@gpoga.com> wrote in message
> news:1141184438.939172.243480@u72g2000cwu.googlegroups.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.
Author
2 Mar 2006 12:00 PM
Phill W.
"cgoetz" <cgo***@gpoga.com> wrote in message
news:1141215329.779029.210210@z34g2000cwc.googlegroups.com...
>
> Phill  W. wrote:
>> "cgoetz" <cgo***@gpoga.com> wrote in message
>> news:1141184438.939172.243480@u72g2000cwu.googlegroups.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.
Author
2 Mar 2006 12:12 PM
cgoetz
Thanks to both of you for taking the time to look into this.  Looks
like its back to the drawing board.  Thanks again.
Author
1 Mar 2006 3:25 PM
Chris Dunaway
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))