Hi all
I'm trying to shade a cell based on a date difference between column 7 & 8.
It seams to work accept column 8 seams to be the one being shaded instead of column 7.
I have googled but can't find a solution to have the calculatin being made on when column 8 being loaded but the formatting being applied to column 7.

Private Sub gvStatus_CellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles gvStatus.CellFormatting
        Dim Closing, Tobemeasured, tobedesign As Date
        Dim NumberOfWeeks As Integer

        If Not IsDBNull(gvStatus.Item(6, e.RowIndex).Value) Then
            Closing = gvStatus.Item(6, e.RowIndex).Value
        End If
        Select Case e.ColumnIndex
            Case 7
                If Not IsDBNull(e.Value) Then
                    Tobemeasured = e.Value
                End If
            Case 8
                If Not IsDBNull(e.Value) Then
                    tobedesign = e.Value
                Else
                    NumberOfWeeks = DateDiff(DateInterval.Weekday, Now(), Closing)
                    If NumberOfWeeks > 5 Then
                        e.CellStyle.BackColor = Color.Red
                    End If
                End If

Recommended Answers

All 5 Replies

When you say "Column 8" is that a 1-based count, or 0-based as in Columns(8)?

I'm only seeing Columns 8 & 9 in your Select statement, unless you changed the DataGridView column indexes to start at 1.

Assuming the DataGridView does indeed have a 0-based column index, your BackColor = Color.Red code can only affect Columns(8) - which would be the 9th column.

I'm a bit confused now...

Thanks John for your reply
It is a 0 based count.
Yes the BackColor = Color.Red it's afecting column 8 but I would like if possible to affect column 7 after checking the date that is on column 8 to make the calculation.

Look at your code again...

Your Select Case e.ColumnIndex ... Case 8 is where the CellStyle.BackColor is set. As currently coded, That code can only affect Columns(8). To affect the previous column, you either have to move the Else statement up to that block, or reference that cell when setting the backcolor.

Thanks John
I have solved it by loading the dates into an Arraylist.
Now when the cell is formatted I will have the dates to be calculated.

Ok, that works - self-solved still counts as a solved thread :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.