1

I have a C# windows forms app with a data grid. One column of the grid is an image column which I put a bitmap in. It works fine on the Windows 10 machine I developed it on and when I deploy it on Windows 7 machines, but on all other Windows 10 machines I get an error. The error is "System.FormatException: Formatted value of the cell has a wrong type." I haven't been able to recreate the error on my development machine, so I'm not sure how to figure out what's wrong. I thought it might be occurring because some prerequisite was not included when I deploy it (it's a Click Once application), but I don't see anything missing. I thought maybe it couldn't find the links to the image, so I tried drawing them instead and still got the same error. I'm guessing it has to do with my bitmap column, but I don't know why it would work on my machine and Windows 7 machines and not other Windows 10 machines. Any ideas on what's happening and/or how to fix this?

Here is the designer for my grid: enter image description here

Here is the DefaultCellStyle settings. I thought the NullValue might be the cause. I originally had System.Drawing.Bitmap in it. I tried using null, but still get an error. enter image description here

Here's the code for how I populate that column.

 Bitmap bmpCombineOnly = new Bitmap(1, 1);

 Bitmap bmpYellow = new Bitmap(26, 26);
 Graphics grYellow = Graphics.FromImage(bmpYellow);
 grYellow.DrawEllipse(new Pen(Color.Yellow, 2), 0, 0, 25, 25);
 grYellow.FillEllipse(new SolidBrush(Color.Yellow), 0, 0, 25, 25);

 bmpCombineOnly = bmpYellow;

 foreach (DataGridViewRow row in ItemsInLocationDataGridView.Rows)
 {
     row.Cells["Icon"].Value = bmpCombineOnly;
 }
boilers222
  • 1,901
  • 7
  • 33
  • 71
  • That NullValue field in the propertygrid is appropriate for text columns but not an image column. Yes it displays the string "System.Drawing.Bitmap", but that is the result of `BitmapInstance.Tostring()`. If you enter some text in the property grid, it will store that string value which is inappropriate for a cell expecting a bitmap value. Find the line in the form's designer file that assigns a string to `Nullvalue and delete it to restore the default. – TnTinMn Mar 07 '19 at 19:51
  • So, you have a Column named `Icon`. Does it even compile with that name? + You're leaking Graphics resources: none of the object you created is being disposed of. I don't get the use of this: `bmpCombineOnly = bmpYellow;`. – Jimi Mar 07 '19 at 20:26

0 Answers0