Monday, December 17, 2007

Prevent DataGridViewImageColumn from showing a red x

When you use a DataGridViewImage column within a DataGridView, a red "x" will show up if you do not anything bound to the row in question. What this means is that if you have an edit row at the end, it will show the red "x". Unfortunately, neither the official documentation for DataGridViewImageColumn in MSDN nor the forum are very clear on this. I gleaned off one of the last posts which recommended creating a new blank bitmap. Instead, I set my cell value to the value of the image I already set up in designer. For a DataGridView that is not getting databound, you will need to put this in the form load.

private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
//assumes that image column is first column
myGrid.Rows[myGrid.Rows.Count - 1].Cells[0].Value =

((DataGridViewImageColumn) myGrid.Columns[0]).Image;
}

8 comments:

  1. This is the only thing that has worked for me. I found several posts about setting DefaultCellStyle.NullValue = null, but that didn't work. Thanks!!

    ReplyDelete
  2. That works great, only minor thing is when you add a row and edit its values the red X appears again until you lose focus from that new row, I'm looking how to fix that now :)

    thanks!

    ReplyDelete
  3. I looked through these solutions and the way I got this to work was to do this after InitializeComponent() call in the constructor to make the default image for data bound rows an empty image

    dataGridView1.Columns[0].DefaultCellStyle.NullValue = null;

    AND hook the RowPrePaintEvent to remove the red X for the empty row at the end of the grid because AllowUserToAddRows = true:

    private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
    if (e.RowIndex >= numberOfRows)
    {
    dataGridView1.Rows[e.RowIndex].Cells[0].Value = null;
    }
    }

    Where numberOfRows is the number of valid rows in the grid. Just spent a few hours dealing with this. Hope this saves someone else the headache...

    ReplyDelete
  4. thanks... worked perfectly

    ReplyDelete
  5. hi , in my case I was using an image column also, but I also use the event CellFormating , and based on some values I was changing the image, i use try catch inside of this and is worked because some of the columns were not ready.

    ReplyDelete
  6. 8 Years later, this post is still very helpful! I maintain a software that inherents a DataGridView. A new requirement was to place Images in that derived DataGridView. I woundered that i only got an red X instead of the Image I want to put in the ImageColumn. The new DataGridView overrides also the CellValueNeeded event and pushes a value from a dataTable in each Cell.
    In the case of the DataGridViewImageColumn the Image was remove and replaced by a random value from the dataTable that results into a red X

    ReplyDelete
  7. Thanks, its lovely to find easy solutions!!

    ReplyDelete