DataGridView Image Column

M

mj2736

I'm trying to display a GIF image in a DataGridViewImageColumn when a
certain condition is true, and nothing when it is false:

private void dgvInbox_DataSourceChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dgvInbox.Rows)
{
DataGridViewImageCell todayCell =
(DataGridViewImageCell) row.Cells["TodayIndicator"];

if (DateTime.Now.Date == (DateTime) row.Cells["ItemDate"].Value)
{
todayCell.Value = Image.FromFile(@"Images\Exclamation.gif");
}
else
{
todayCell.Value = null;
}
}
}

When stepping through the code, the appropriate lines to set the cell
value are being hit, but in all cases (both true and false) the default
[X] graphic is displayed. I know the file path is correct because I do
not get a FileNotFoundException. I've tried setting the ValueIsIcon and
ValueType properties of the cell and of the column, but I've not yet
found a combination that works.

Thanks!
MJ
 
M

mj2736

I got this working by moving my code into the DataBindingComplete event
handler (instead of DataSourceChanged). Also, instead of assigning
'null' to the cell value when I want no image, I am using:

todayCell.Value = new Bitmap(1, 1);

This meets my requirements. Please let me know if there is a better or
more correct way to do this.

-MJ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top