Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Appearance of Null / Empty Image on DataGridView

Tags:

c#

winforms

I havea WinForms application with several datagridviews. On one of them I have a column which dynamically populates with images. I have included a screenshot below:

Screenshot

Many of the cells do not meet the requirements to generate an image and so appear empty. However, when this happens the application shows the default red X for no image. I want the cells to simply appear empty rather than show this default image.

I have tried creating a blank image, but this causes its own issue; namely, because my datagridview has alternating row back colors, a white image shows up against grey rows, and vice versa.

In effect I want a null / empty or transparent image, that simply does not show at all in the column, when the run time requirements for one of the other images are not met.

For completeness the code I am using at the moment is as follows:

foreach (DataGridViewRow row in dataCaseHistory.Rows)
{                    
    DataGridViewCell cell = row.Cells["DocID"];
    if (cell.Value.ToString().Length > 0)
    {
        if (((int)cell.Value) % 2 == 0)
        {
            row.Cells["Doc"].Value = (System.Drawing.Image)Properties.Resources.Paperclip___done;
        }
        else
        {
            row.Cells["Doc"].Value = (System.Drawing.Image)Properties.Resources.Bundle___done;
        }
    }
    else
    {
         row.Cells["Doc"].Value = null;
    }
}

The logic tests to select the image is temporary. I'm just trying to set things up before applying the real, more involved criteria for image selection.

like image 247
PJW Avatar asked Dec 06 '12 13:12

PJW


1 Answers

try this:

After the InitializeComponent() call inside the constructor put this:

yourGrid.Columns["Doc"].DefaultCellStyle.NullValue = null;
like image 73
Tobia Zambon Avatar answered Nov 04 '22 09:11

Tobia Zambon