473,395 Members | 1,583 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,395 software developers and data experts.

Invalid Cast Errors

I keep getting an invalid cast error on this line of code:

Dim gridCol As MyGridColumn = CType(dgt.GridColumnStyles(hi.Column),
MyGridColumn)

I am trying to develop a sub that will change the colors on a cell that
is clicked on in a DataGrid control.

Here is the full code:

Private Sub PaintCell(ByVal sender As Object, ByVal e As
MouseEventArgs)

Try
Dim hi As DataGrid.HitTestInfo
Dim grid As DataGrid = CType(sender, DataGrid)
hi = grid.HitTest(e.X, e.Y)
If hi.Type = DataGrid.HitTestType.Cell Then
Dim dgt As DataGridTableStyle =
dgvBranches.TableStyles(0)
Dim cm As CurrencyManager =
CType(Me.BindingContext(myDataSet.Tables(0)), CurrencyManager)
Dim cellRect As Rectangle
cellRect = grid.GetCellBounds(hi.Row, hi.Column)
Dim gridCol As MyGridColumn =
CType(dgt.GridColumnStyles(hi.Column), MyGridColumn)
Dim g As Graphics = dgvBranches.CreateGraphics()
Dim fBrush As New SolidBrush(Color.Blue)
Dim bBrush As New SolidBrush(Color.Yellow)
gridCol.PaintCol(g, cellRect, cm, hi.Row, bBrush,
fBrush, False)
End If
Catch ex As Exception
LogErrors(ex, "PaintCell")
End Try
End Sub 'PaintCell
Any help would be appreciated. Thanks!

Apr 28 '06 #1
4 1386
What data type is MyGridColumn? Does it inherit from another class?
Is the class it inherits from a DataGridColumnStyle? The
dgt.GridColumnStyles collection is a collection of DataGridColumnStyle
so if your gridCol is not a DataGridColumnStyle or a class that
inherits from it, the cast wont work.

Also, I noticed that your sub create a number of objects that need to
be disposed, namely the Graphics object and the Brush objects. You
should call .Dispose on these objects to free up their unmanaged
resources.

Apr 28 '06 #2
Sorry I knew there was something I forgot to add to this:

Public Class MyGridColumn
Inherits DataGridTextBoxColumn
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

Perhaps that will answer your questions Chris.

Apr 28 '06 #3
Needless to say I still get the error.

Apr 28 '06 #4
Are the items in dgt.GridColumnStyles in fact instances of
MyGridColumn? Can you verify using the debugger what type
dgt.GridColumnStyles(hi.Column) is? If it is a MyGridColumn instance
then I don't know why the cast is not working.

If if is an instance of one of the base classes (DataGridTextBoxColumn
or DataGridColumnStyle) then the cast won't work because you can't cast
from a base type to a derived type unless the object is, in fact, an
instance of the derived type.

PS. I still stand by recommendation to Dispose your brushes and
Graphics instances.

Apr 28 '06 #5

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

Similar topics

1
by: | last post by:
When I execute the following (with an OleDBDataAdapter), wanting to add a row to a visual foxpro table: myrow= datasetTarget.Tables(0).NewRow 'fill all columns here like.. row(i)= myvalue '...
6
by: Thomas Barth | last post by:
Hi, I'm new to windows programming and still reading a book about windows-programming with C++. I copied the following code from the book into my ide (Eclipse/CDT) to comprehend the code, but two...
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...
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...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
2
by: James | last post by:
I'm a newbie to vb.net, and classes were something I never played with in "standard" vb, but... Basically,part of my program comprises of some tcp/ip stuff, and I used the following code to...
1
by: Ratz | last post by:
Hello everyone! I'm new to this Forum! I've spent about 56 hours trying to solve a .NET VB Runtime error while Using a program.Ive contacted the developer of the program and he could not figure out...
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.