I have an ImageColumn
on my DataGridView
that acts according to another hidden cell called "HasWarnings". On form Load event I refresh this DataGridView so for each row, if HasWarning
is true the warning image and otherwise a success image is displayed. I also have a Refresh button on top of my DataGridView
.
However, the images are not displayed on Form Load
event and I get the red cross image. It is only when I press on the Refresh button that they come back. What surprises me is that by clicking on the Refresh button the very same function as the Load event is called. Below is the code to the Refresh method that is called both on Load event and on the button click:
public void RefreshHistory()
{
pnlOverview.Visible = false;
pnlNoHistory.Visible = false;
try
{
using (var db = new Entities(cs)
{
var linqHistory = db.Histories.Select(h => new
{ h.Id, h.RunDate, h.HasWarnings }).OrderByDescending(h => h.RunDate).Take(500);
if (linqHistory.Any())
{
dgvHistory.DataSource = linqHistory.ToList();
dgvHistory.Columns["Id"].Visible = false;
dgvHistory.Columns["HasWarnings"].Visible = false;
dgvHistory.Columns["RunDate"].HeaderText = "Date/Time";
pnlOverview.Visible = true;
dgvHistory.Rows[0].Selected = true;
long reportId = Convert.ToInt64(dgvHistory.Rows[0].Cells["Id"].Value);
SetWarningImages();
SetReportDetails(reportId);
}
else pnlNoHistory.Visible = true;
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
}
And this is SetWarningImages()
method that assigns the corresponding image to each row in the DataGridView
:
private void SetWarningImages()
{
foreach (DataGridViewRow row in dgvHistory.Rows)
{
bool hasWarnings = (bool)row.Cells["HasWarnings"].Value;
if (hasWarnings)
((DataGridViewImageCell)row.Cells["HasWarningsImage"]).Value =
Properties.Resources.warning16;
else
((DataGridViewImageCell)row.Cells["HasWarningsImage"]).Value =
Properties.Resources.success16;
}
}
I am wondering why this very code does not display the images (but the data is displayed correctly) on Load
but works on the button_click
?
N.B. Adding the lines dgvHistory.Refresh();
or dgvHistory.PerformLayout();
is not helping either.