Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Stephens' Visual Basic Programming 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' Visual Basic Programming 24-Hour Trainer by Rod Stephens; ISBN: 978-0-470-94335-9
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' Visual Basic Programming 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 2nd, 2014, 12:08 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
Default Datagridview only shows empty tables!

Hi I am trying to apply your tyrit example in lesson 39 to my application. the problem is if I connect the datagridview to an empty table, it shows the column headers just fine, but if I connect it to a filled table, it gives the error that:
Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.

The error occurs on the last line of this code:
Code:
        'Get the selected airplane
        Dim plane As Airplane =
        DirectCast(cmbLineNumberTo.SelectedItem, Airplane)

        'Display the airplane information
        dgvAPList.DataSource = plane.AirplaneStallAssignments
I appreciate your help!
  #2 (permalink)  
Old May 2nd, 2014, 01:27 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Hi Shabnam,

Quote:
Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.
Code:
'Get the selected airplane         Dim plane As Airplane =         DirectCast(cmbLineNumberTo.SelectedItem, Airplane)

'Display the airplane information         
dgvAPList.DataSource = plane.AirplaneStallAssignments
I don't think I understand this. If you don't get an error on the first line, then the selected object must be an Airplane as you intend.

I'm guessing that AirplaneStallAssignments is a collection or table (something that is enumerable)? I don't see how making that assignment should lead to that particular "unable to cast" error.

Do you have any event handlers registered for the dgvAPList control? Perhaps setting its DataSource makes it raise an event and it is actually the event handler that is throwing the exception?

The other thing that might make sense is if there's an automatically generated event handler somewhere (perhaps created by EF). If it's code is marked with the right attributes, it wouldn't show up in the debugger, but it could still throw this exception.

You might try searching the project for DataGridViewCellEventArgs and see if you find anything.

If you can't find a likely cause for this, you can zip the project up (without the bin and obj directories) and attach it to this post (or email it to me) and I'll try to take a closer look.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old May 2nd, 2014, 01:34 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
Default

yes; you were exactly right. I had this:
Code:
Private Sub dgvAPList_CellContentClick(ByVal sender As Object, _
    '    ByVal e As DataGridViewCellEventArgs) Handles dgvAPList.CellClick
I removed this handler and everything works fine; but I still need this handler. Would you please explain how I would have the cell click even handler with a datasource connected to my grid view?

Thanks
  #4 (permalink)  
Old May 2nd, 2014, 01:45 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Huh. Your code doesn't have that apostrophe on the second line, right? And you're registering it with the Handles clause, not an AddHandler statement?

I think the CellClick event handler should take a DataGridViewCellEventArgs as a parameter, so I don't see what's wrong with your code.

You might try commenting out the event handler and then creating a new one by using the Properties window or dropdowns in the code editor, just to see if it gives you something with the same parameters.

If that doesn't work, I would try changing the e parameter's data type to System.EventArgs. Then set a breakpoint inside the event handler so you can see what is in that parameter when the event handler executes. For example, you can type this in the Immediate window to see what type the e parameter really has:

Code:
?TypeName(e)
Or you can hover over it to see what values it contains.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
shabnamzangeneh (May 6th, 2014)
  #5 (permalink)  
Old May 2nd, 2014, 01:58 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2014
Posts: 19
Thanks: 4
Thanked 0 Times in 0 Posts
Default

how funny; I un-commented the event handler and now it works. I am not quite sure what was wrong before. it is the same event handler (originally it didn't have that apostrophe on the second line).

Anyway, thank you very much for your help. I truly appreciate that.
  #6 (permalink)  
Old May 3rd, 2014, 12:34 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

As long as it works!
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #7 (permalink)  
Old May 6th, 2014, 09:24 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

The exception tells you exactly what is wrong. Your event handler event args is of type System.Windows.Forms.DatagridCellEventArgs and the type of events that the framework expects is of type System.EventArgs so it throws a cast exception. Change the type of the event args to System.EventArgs and you will fix it.
The Following User Says Thank You to mmorgan30 For This Useful Post:
shabnamzangeneh (May 6th, 2014)


Similar Threads
Thread Thread Starter Forum Replies Last Post
no image shows i_m_9415 BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 May 12th, 2010 02:57 AM
Manifest Editor - Application tab shows empty page sunrain BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 6 January 21st, 2009 04:42 PM
[2005] GridView - empty cell shows no border snufse ASP.NET 2.0 Basics 2 July 29th, 2008 02:49 PM
Combobox that shows what's not in its list Mitch Access 6 September 18th, 2003 11:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.