1

My Form has a DataGridView control and Column 0 is of type DataGridViewImageColumn.
When the Form is first displayed, an empty DataGridView control is displayed and Column 0 has a rectangle with a red X in it.
The same icon is displayed when I add a row to the control.

How do I get rid of the box with red X and display nothing?

I have tried the following. Globals.ERROR_LIST_SEVERITY has a value of 0.

dataGridView_ErrorList.Columns[Globals.ERROR_LIST_SEVERITY].DefaultCellStyle.NullValue = null;

Also tried brute force just to see if it would work:

dataGridView_ErrorList.Rows[0].Cells[0].Value = null;

Any idea why this doesn't work? Do I need to create a blank image and assign the value to the blank image?
That seems silly when setting the null value should work.

Jimi
  • 29,621
  • 8
  • 43
  • 61
Dan Z
  • 101
  • 13
  • 1
    Read the Remarks section here: [DataGridViewImageColumn Class](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagridviewimagecolumn?redirectedfrom=MSDN&view=netframework-4.8#remarks). You'll need a custom `DataGridViewImageCell` and a `DataGridViewImageColumn` to set its `CellTemplate` to the custom cell. You can easily add it in code instead of using the DGV's Designer, and the result is not a hassle as is subscribing to the `NewRowAdded` event. – Jimi May 03 '19 at 22:28
  • 1
    Thank you so much,Jimi. I missed those remarks on Microsoft's website. Actually the solution was much easier than I originally thought because I only needed to set the AllowUserToAddRows Property to False in the designer. The DGV is only populated by code so that Property needed to be set anyway. My code assigns the image when it adds a new row, so the image can never be null. Thanks for leading me to the solution! Dan – Dan Z May 03 '19 at 22:59

2 Answers2

0

As described in the MSDN Docs, in the Remarks section of the DataGridViewImageColumn Class, to replace the default Error Image with a custom Image, requires a custom class derived from DataGridViewImageCell.
This allows to override the DefaultNewRowValue property, usually read-only, setting the custom Image. Also, eventually, to override the PaintErrorIcon method.

This is an implementation of custom DataGridViewImageColumn, coupled with a custom DataGridViewImageCell, which can be configured to show a different default Image when no Image is specified or the bound field value is null.

If no image is specified, the cell will show an empty content, also in the DataGridView New Row.

The class constructor has several overloads. You can specify:

  • The Column Name (defaults to ImageColumn).
  • The Column's Header text (defaults to ImageColumn).
  • The DataPropertyName, if the Column will be bound to a DataSource field (defaults to string.Empty).
  • The Image used as placeholder when no image is present (defaults to an empty Bitmap)

To add the custom Image column to a DataGridView, use the DataGridView.Columns.Add() method, passing a new DGVCustomImageColumn, initialized as needed.
Here, I'm just setting the Column.Name, the HeaderText and the DataPropertyName:

dataGridView1.Columns.Add(new DGVCustomImageColumn("ImageCol", "Image", "ImageField"));

The DGVCustomImageColumn class:

using System.Drawing;
using System.Windows.Forms;

class DGVCustomImageColumn : DataGridViewImageColumn
{
    private Bitmap dgvErrorBitmap = new Bitmap(1, 1);
    public DGVCustomImageColumn()
        : this("ImageColumn", "ImageColumn", string.Empty, null) { }
    public DGVCustomImageColumn(string colName, string headerText)
        : this(colName, headerText, string.Empty, null) { }
    public DGVCustomImageColumn(string colName, string headerText, string dataField)
        : this(colName, headerText, dataField, null) { }

    public DGVCustomImageColumn(string colName, string headerText, string dataField, Bitmap errorImage)
    {
        this.CellTemplate = new CustImageCell(errorImage ?? dgvErrorBitmap);
        this.DataPropertyName = dataField;
        this.HeaderText = headerText;
        this.Image = errorImage ?? dgvErrorBitmap;
        this.Name = colName;
    }

    protected class CustImageCell : DataGridViewImageCell
    {
        public CustImageCell() : this(null) { }
        public CustImageCell(Bitmap defaultImage) => this.DefaultNewRowValue = defaultImage;
        public override object DefaultNewRowValue { get; }
    }
}
Jimi
  • 29,621
  • 8
  • 43
  • 61
0

I have encountered this problem, but I overcome it with the following statement

dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;
dataGridView1.Rows[0].Cells[0].Value = null;
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
mohamad
  • 11
  • 2