0

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.

Alex H
  • 1,424
  • 1
  • 11
  • 25
disasterkid
  • 6,948
  • 25
  • 94
  • 179

1 Answers1

1

The order of events sometimes will force you to delay some things a little until the Form is Shown and its Layout fully complete.

Therefore it often helps to move a call to maybe the Form.Shown or Form.Layout events. Note that Form.Layout may happen more often than you like so Form.Shown is probably preferrable, unless you want to use a flag..

In this and this post there are interesting discussions abut the topic..

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111