Search code examples
c#datagridviewcell-formatting

Exception unhandled when I want to set value to cell


I am using query to get the data and display on datagridview. And using cell formatting to call a function (Colouring the row when condition happen) Then I want to set a cell value when some condition happen. The code are

    private void InventoryListGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        HighlightRow(sender, e.RowIndex, "Equipment");
    }

    private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        HighlightRow(sender, e.RowIndex, "Chemical");
    }
    private void HighlightRow(object sender, int index, string list)
    {
        DataGridView row = (sender as DataGridView);
        if (row[0, index].Value == null) return;

        Color bgColor = Color.LightGreen; //available
        Color foreColor = Color.Black;

        // status column
        if (row[4, index].Value.ToString() == "CHECKOUT")
        {
            bgColor = Color.Yellow; // Color.FromArgb(128, 128, 255);
        }

        // calibrate due
        if (bgColor == Color.LightGreen)
        {
            DateTime dueDate = Convert.ToDateTime(row[6, index].Value);
            DateTime today = DateTime.Now;
            TimeSpan diff = dueDate - today;
            if (diff.TotalDays <= 0)
            {
                bgColor = Color.Red;
                foreColor = Color.LightYellow;
                row[4, index].Value = list == "Chemical" ? "EXPIRED" : "OVERDUE";//************It shows this -> System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.'

            }
            else if (diff.TotalDays <= 30)
            {
                if (list == "Chemical")
                {
                    bgColor = Color.LightGreen;
                }
                else
                {
                    bgColor = Color.LightCoral; // Color.FromArgb(255, 128, 128);
                }
            }
            else if (diff.TotalDays <= 90)
            {
                if (list == "Chemical" && diff.TotalDays <= 60)
                {
                    bgColor = Color.Orange;
                }
                else if (list == "Chemical")
                {
                    bgColor = Color.LightGreen;
                }
                bgColor = Color.Orange; // Color.FromArgb(255, 192, 128);
            }
        }

        System.Diagnostics.Debug.WriteLine("Coloring " + row[0, index].Value.ToString() + " with " + bgColor);
        // start coloring the row
        for (int i = 0; i < row.ColumnCount; i++)
        {
            row[i, index].Style.BackColor = bgColor;
            row[i, index].Style.ForeColor = foreColor;
        }
    }

The code below has exception unhandled?

row[4, index].Value = list == "Chemical" ? "EXPIRED" : "OVERDUE";

How to set the value for the cell?


Solution

  • I change to use another method which is using query to set the value first to display on the datagridView instead of changing the value after data displayed in datagridView.