DataGridView style change not firing

Jay O'Brien 71 Reputation points

I have a DataGridView that I source from a datatable and then read through, changing the style in certain cells. It is reaching the style change row, but not changing the style.

If I change sAlert to happen when a button is clicked it works. How do I get it to work as part of the load?

        private void CDGrid_Load(object sender, EventArgs e)
            lblCo.Text = Global.gCompany;
            Text = Text + "  " + Global.gCompany;
            dtCD = ModComFunct.Populate("SELECT * FROM CD ORDER BY Bank, ExpirDate");
            DgvCD.DataSource = dtCD;
            mtbCk.Text = ModComFunct.FGetACHCA();
            mtbInt.Text = ModComFunct.FGetReqAcctNo("Interest Received");
            foreach (DataRow FoundRow in dtCD.Rows)
                ResultIndex = cbBank.FindStringExact(Convert.ToString(FoundRow["Bank"]), ResultIndex);
                if (ResultIndex == -1)

            dtpDate.Value = DateTime.Today;
            Pur1st = 1;

        private void sAlert()
            DataGridViewCellStyle Style = new DataGridViewCellStyle();
            Style.Font = new Font(DgvCD.Font, FontStyle.Bold);
            Style.BackColor = Color.Red;
            Style.ForeColor = Color.White;
            foreach (DataGridViewRow row in DgvCD.Rows)
                if (Convert.ToDateTime(row.Cells["ExpirDate"].Value) < DateTime.Today.AddDays(31))
                    row.Cells["ExpirDate"].Style = Style;
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,875 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,633 questions
0 comments No comments
{count} votes

Accepted answer
  1. Davin Mickelson 121 Reputation points

    Hi, Jay.
    I wonder if you should put your code in the CellFormatting event handler for the DataGridView control.
    I'm unsure of your skill level, so I apologize if the steps below are too simple.

    • Select the DataGridView control in the designer.
    • Click the lightning bolt at the top of the Properties window to show the events.
    • Double-click the CellFormatting event to create the event handler function.

    They do it here. Maybe you can get an idea from their source code.

    Good luck.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful