Hi Tom. Thanks for responding. Below is the original code I got from
the msdn site which I built my own code off of:
cellRect = grid.GetCellBounds(hi.Row, hi.Column)
' Get the clicked DataGridTextBoxColumn.
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)
' Get the Graphics object for the form.
Dim g As Graphics = dataGrid1.CreateGraphics()
' Create two new Brush objects: a fore brush and back brush.
Dim fBrush As New SolidBrush(Color.Blue)
Dim bBrush As New SolidBrush(Color.Yellow)
' Invoke the Paint method to paint the cell with the brushes.
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush, fBrush,
False)
Public Class MyGridColumn
Inherits DataGridTextBoxColumn
Public Sub PaintCol(g As Graphics , cellRect As Rectangle , _
cm As CurrencyManager , rowNum As integer , bBrush as Brush , _
fBrush As Brush , isVisible As Boolean )
me.Paint(g, cellRect, cm, rowNum, bBrush, fBrush, isVisible)
End Sub
In the above dgt.datagridcolumnstyles(0) points to a
datagridtextboxcolumn object as it does in my porgram as well. Using
the exact name of the object instead of this reference produced the
same result
In the statement:
Dim gridCol As MyGridColumn = CType _
(dgt.GridColumnStyles(hi.Column), MyGridColumn)
We are attempting to convert a datagridtextboxcolumn (Parent object)
into a MyGridColumn object (child) in order to use the paint method.
Is the above code incorrect? Again, I guess I had the wrong idea on
inheritance. I thought its point was to add functionality to more
basic objects.
You can find the above code at:
http://msdn.microsoft.com/library/de...painttopic.asp
Please tell me if I am missing something.
Tom Shelton <to*@mtogden.com> wrote in message news:<Oe**************@TK2MSFTNGP11.phx.gbl>...
In article <8b**************************@posting.google.com >, Mike Cooper wrote: There is something about inherited classes I evidently don't know...
I wrote the following class:
Class Class1
inherits System.Windows.Forms.DataGridTextBoxColumn
End Class
There is absolutely no added functionality to it.
Then, in Sub Main, I have the following code.
Dim hihi as Datagridtextboxcolumn
Dim hehe as Class1
hehe = ctype(hihi,class1)
The above code failes with a cast not valid error!!
My real code is considerably more complex ofcourse. But in order to
find out
why my "cast is not valid" I have reduced it to the skeleton code
above. Because class1 is a undistinguished child of
DataGridTextBoxColumn, I see no reason why an object of the parent
cannot be Ctyped into an object of the child. But it can't. Can
anyone tell me why. I am using Visualbasic.net if that makes a
difference. Please let me know.
Thanks,
Mike Cooper
You are attempting to downcast the parent object to the childs type.
That is not possible. Basically, you have to think of it as your C1 is
a DataGridTextBoxColumn, but a DataGridTextBoxColumn is not a C1. Now,
if you reverse it...
Dim hihi as DataGridTextBoxColumn
Dim hehe as New C1
hihi = CType(hee, DataGridTextBoxColumn)
That will work. If you think about it, it makes sense. When you
inherit from something it is normally to extend that functionality. If
you were able to cast hihi to hehe - you wold end up with a lot of
problems if you started calling C1 functionality, because it isn't
implemented in the parent class.
HTH