views:

1169

answers:

2

I've created a DataGridView and added a DataGridViewImageColumn (using the Designer).

The DataGridView has "AllowUserToAddRows = true" so a blank row is displayed for the user to enter a new row in. In this blank row the DataGridViewImageColumn displays a red x. I want to always display the same image in the column regardless of whether the row has any data or not (I am binding the cell's click event so using the DataGridViewImageColumn as a button).

How do I get rid of the red x?

+1  A: 

I've found a solution, but I'm not sure it's the best way.

I override the RowsAdded event and set the Value of the DataGridViewImageColumn to null. I think since the value is null, it displays the image.

private void dgvWorksheet_RowsAdded(object sender, 
     DataGridViewRowsAddedEventArgs e)
{
    dgvWorksheet.Rows[e.RowIndex].Cells[colStartClock.Index].Value = null;
}

I also set the NullValue of the Column to null in the Form_Load

colStartClock.DefaultCellStyle.NullValue = null;

I'm not sure if there's anything else I need to do. It seems to be working but it seems a bit buggy - random clicking sometimes results in exceptions, so more investigation is needed.

Matt Frear
+1  A: 

I use an ImageList to hold all my images, but here's a trimmed down version that should help. You're right to set the Value, but you should set it to the null-equivalent of a bitmap, which is just a static blank image. I use:

e.Value = (imageIndex != int.MinValue) ? imageList.Images[imageIndex] : nullImage;

where nullImage is defined earlier in the class as:

private readonly Image nullImage = new Bitmap(1, 1);
Chris Doggett