View Single Post
Old May 2nd, 2014, 01:27 PM
Rod Stephens's Avatar
Rod Stephens Rod Stephens is offline
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

Hi Shabnam,

Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.DataGridViewCellEventArgs'.
'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 Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)