DataGridView: System.InvalidCastException, Value is not a convertible object

View: New views
3 Messages — Rating Filter:   Alert me  

DataGridView: System.InvalidCastException, Value is not a convertible object

by m0n0 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello there,

is isn't just a General question but I think it fit's best here...

I'm using WinForm and I add custom objects to a datagrid, after that I get this exception in the Paint routine of the datagrid:

Unhandled Exception: System.InvalidCastException: Value is not a convertible object: MyNamespace.MyClass to System.String
  at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000]
  at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000]
  at System.Windows.Forms.DataGridViewCell.get_FormattedValue () [0x00000]
  at System.Windows.Forms.DataGridViewRow.PaintCells (System.Drawing.Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts) [0x00000]
  at System.Windows.Forms.DataGridViewRow.Paint (System.Drawing.Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow) [0x00000]
  at System.Windows.Forms.DataGridView.OnPaint (System.Windows.Forms.PaintEventArgs e) [0x00000]
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000]
  at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000]
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000]
  at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000]
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext context) [0x00000]
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form mainForm) [0x00000]
  at Alias.Program.Main (System.String[] args) [0x00000]

i never saw such a problem with MS .Net, but I had overloaded the ToString method to show what I want in the datagrid:

public override string ToString()
{
  return ("What I want!");                          
}
that worked well with M$...

now mono makes problem, does anyone of you have a workaround?

I also implemented the
public static implicit operator String( MyClass t )
{
 return t.ToString();
}
to get around the problem, but that doesn't work, too.

Re: DataGridView: System.InvalidCastException, Value is not a convertible object

by Stifu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is this with Mono 2.4.x?
If so, it'd be nice if you could create a reduced test case and file a bug report.

m0n0 wrote:
Hello there,

is isn't just a General question but I think it fit's best here...

I'm using WinForm and I add custom objects to a datagrid, after that I get this exception in the Paint routine of the datagrid:

Unhandled Exception: System.InvalidCastException: Value is not a convertible object: MyNamespace.MyClass to System.String
  at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000]
  at System.Convert.ChangeType (System.Object value, System.Type conversionType, IFormatProvider provider) [0x00000]
  at System.Windows.Forms.DataGridViewCell.get_FormattedValue () [0x00000]
  at System.Windows.Forms.DataGridViewRow.PaintCells (System.Drawing.Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts) [0x00000]
  at System.Windows.Forms.DataGridViewRow.Paint (System.Drawing.Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow) [0x00000]
  at System.Windows.Forms.DataGridView.OnPaint (System.Windows.Forms.PaintEventArgs e) [0x00000]
  at System.Windows.Forms.Control.WmPaint (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.DataGridView.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000]
  at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000]
  at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000]
  at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000]
  at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000]
  at System.Windows.Forms.Application.Run (System.Windows.Forms.ApplicationContext context) [0x00000]
  at System.Windows.Forms.Application.Run (System.Windows.Forms.Form mainForm) [0x00000]
  at Alias.Program.Main (System.String[] args) [0x00000]

i never saw such a problem with MS .Net, but I had overloaded the ToString method to show what I want in the datagrid:

public override string ToString()
{
  return ("What I want!");                          
}
that worked well with M$...

now mono makes problem, does anyone of you have a workaround?

I also implemented the
public static implicit operator String( MyClass t )
{
 return t.ToString();
}
to get around the problem, but that doesn't work, too.

Re: DataGridView: System.InvalidCastException, Value is not a convertible object

by m0n0 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Stifu wrote:
Is this with Mono 2.4.x?
If so, it'd be nice if you could create a reduced test case and file a bug report.
No this isn't with v. 2.4... it's about mono 1.9 (dunno how to get an .deb file for mono 2.4 from mono page... and my distr. ain't got a newer version in the apt repositories....

So maybe that would solve the problem, but I worked around it, too.

I implemented the Interface IConvertible and programmed the ConvertToString Function...  Then that worked. But I'm not understanding why mono isn't calling to string on the object to convert it to a string...

I'm not going to offer a test case, firstly because it isn't known if it's also happening in 2.4... But the the problem appeared with an Datagrid that contains a "DataGridViewComboBoxCell" or something like that ^^

each item in the combobox has it's string value assigned  AND also it's tag... the tag is a custom object, the one that mono complaint's about in the exception.