DataGridView style change not firing

Jay O'Brien 71 Reputation points
2023-05-18T17:13:16.1666667+00:00

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;
            SFillBroker();
            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)
                    cbBank.Items.Add(Convert.ToString(FoundRow["Bank"]));
            }

            SAdjustGrid();
            dtpDate.Value = DateTime.Today;
            Pur1st = 1;
            sAlert();
        }

        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,632 questions
C#
C#
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,293 questions
0 comments No comments
{count} votes

Accepted answer
  1. Davin Mickelson 116 Reputation points
    2023-05-18T17:38:28.24+00:00

    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.
    https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-customize-data-formatting-in-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8

    Good luck.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful