Click here to Skip to main content
14,239,876 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am writing following code after every filtering of Dataview. How to avoid this repetation of code?

For i As Integer = 0 To datagridview1.Rows.Count - 1
            If (datagridview1.Rows(i).Cells("Completion Date").Value <= Date.Today) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
                datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Red
            ElseIf (DateDiff(DateInterval.Day, Date.Today, datagridview1.Rows(i).Cells("Completion Date").Value) <= 20) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
                datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Aqua
            End If
        Next


What I have tried:

Module Module1
    Public datagridview1 As DataGridView
    Sub Gridviewcolor()

        For i As Integer = 0 To datagridview1.Rows.Count - 1
            If (datagridview1.Rows(i).Cells("Completion Date").Value <= Date.Today) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
                datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Red
            ElseIf (DateDiff(DateInterval.Day, Date.Today, datagridview1.Rows(i).Cells("Completion Date").Value) <= 20) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
                datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Aqua
            End If
        Next

    End Sub
End Module
Posted
Updated 9-Jul-19 9:15am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Move it to the CellPainting event: Colouring DataGridView Cells According to their Content in WinForms[^] - the code is in C# but it's pretty simple, and the same actions in VB. Online translators may help if you can;t work it out for yourself: Code Converter C# to VB and VB to C# – Telerik[^]
   
Comments
Maciej Los 8-Jul-19 14:31pm
   
5ed!
Sree1981 8-Jul-19 15:03pm
   
Thanks for your reply.But I couldn't succeed.First of all its not matching with my concept.I need to check values of one column based on a value on another column.So I replaced that with my code but when I call it using dgvData_CellPainting() it shows error.
OriginalGriff 8-Jul-19 15:30pm
   
And without seeing your code or knowing what the error is, what do you expect us to do?
Sree1981 9-Jul-19 15:54pm
   
I put following code in the form which is having datagridview1

Sub Gridviewcolor(datagridview1 As DataGridView)
For i As Integer = 0 To datagridview1.Rows.Count - 1
If (datagridview1.Rows(i).Cells("Completion Date").Value <= Date.Today) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Red
ElseIf (DateDiff(DateInterval.Day, Date.Today, datagridview1.Rows(i).Cells("Completion Date").Value) <= 20) And (datagridview1.Rows(i).Cells("Status").Value = "Ongoing") Then
datagridview1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Aqua
End If
Next
End Sub

and I called this with Gridviewcolor(DataGridView1)
its working but someone told this is not correct.
Sree1981 yesterday
   
I put my code in cellpainting event of datagridview1 as follows.Its working but Is it correct?

Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If (DataGridView1.Rows(i).Cells("Completion Date").Value <= Date.Today) And ((DataGridView1.Rows(i).Cells("Status").Value = "Ongoing") Or (DataGridView1.Rows(i).Cells("Status").Value = "Hold")) Then
DataGridView1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Red
ElseIf (DateDiff(DateInterval.Day, Date.Today, DataGridView1.Rows(i).Cells("Completion Date").Value) <= 20) And ((DataGridView1.Rows(i).Cells("Status").Value = "Ongoing") Or (DataGridView1.Rows(i).Cells("Status").Value = "Hold")) Then
DataGridView1.Rows(i).Cells("Completion Date").Style.BackColor = Color.Aqua
End If
Next
End Sub
OriginalGriff yesterday
   
No. Not even close.
What do you think the CellPainting event is there for? When do you think it is called? Hint: think about the event name ...
Sree1981 yesterday
   
Really I don't know I am a beginner.
OriginalGriff yesterday
   
Well, have you checked the documentation?
When do *you* think it might be called with a name like that?
Sree1981 23hrs ago
   
Yes, I did. I replaced dgvData_CellPainting (in the article which you referred) with DataGridView1_CellPainting, thinking that dgvData is the name of the Datagridview.
OriginalGriff 23hrs ago
   
No, when do you think the system calls teh handler method for teh event called CellPainting?

Is it:
1) When the form is first constructed?
2) When the DGV is constructed?
3) When the mouse goes over it?
4) When the user clicks on a specific cell?
5) Other?

Which do you think, and why?
Sree1981 22hrs ago
   
Initially, I thought it was a procedure. I tried in that way but there was no code for calling the procedure.
Then as per the instruction "Just hook up the CellPainting event handler to your DataGridView" I put my code in the Cellpainting event.
OriginalGriff 21hrs ago
   
This has "long day" written all over it ...

Events: you do know what they are, yes?
When you click a button, the system raises a Click event, which checks the specific button handler collection and calls each handler method in the chain in turn.
To "hook up a handler" the easiest way is to double click the Event in the Events pane of the object panel in Visual studio (look for a lighting bolt just above the properties list).
Your method will be called when the Event happens.

In this case, you need to find the CellPainting Event in the list and add the code to the handler.
But not that code. That code is a loop, and that isn't necessary in the CellPainting event because ... well you tell me: what does the CellPainting event handler get called for? What triggers the system to raise the event?
Sree1981 16hrs ago
   
I read the following...
The CellPainting event is called whenever the cell needs to be repainted. Things that can cause this are the cell needing to redraw because it was invalidated, covered by another window, the data source changes, the cell value changes, cell came into view after being scrolled, etc
OriginalGriff 15hrs ago
   
Yes - exactly.
So why would you want to put a loop in there to look at other rows?
Sree1981 10hrs 15mins ago
   
I hope I am on the right path this time.But unable to restrict the colouring only when the "status" column value is ""Ongoing" or "Hold".
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

Dim dgv As DataGridView = TryCast(sender, DataGridView)
If dgv IsNot Nothing AndAlso e.RowIndex >= 0 Then
If e.ColumnIndex = dgv.Columns("Completion Date").Index Then
Dim c As Color
If e.Value IsNot Nothing Then
Dim value As Date = CDate(e.Value)
If value < Today Then
c = Color.Red
ElseIf DateDiff(DateInterval.Day, Date.Today, value) <= 25 Then
c = Color.Aqua
Else
c = dgv.DefaultCellStyle.BackColor
End If
e.CellStyle.BackColor = c
End If
End If
End If
End Sub
Sree1981 9hrs 10mins ago
   
I got following code working.Is it correct?
Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting

Dim dgv As DataGridView = TryCast(sender, DataGridView)
If dgv IsNot Nothing AndAlso e.RowIndex >= 0 Then
If e.ColumnIndex = dgv.Columns("Completion Date").Index Then
Dim c As Color
If e.Value IsNot Nothing Then
Dim value As Date = CDate(e.Value)
If value < Today AndAlso dgv.Rows(e.RowIndex).Cells("Status").Value.ToString = "Ongoing" Or dgv.Rows(e.RowIndex).Cells("Status").Value.ToString = "Hold" Then
c = Color.Red
ElseIf DateDiff(DateInterval.Day, Date.Today, value) <= 25 AndAlso dgv.Rows(e.RowIndex).Cells("Status").Value.ToString = "Ongoing" Or dgv.Rows(e.RowIndex).Cells("Status").Value.ToString = "Hold" Then
c = Color.Aqua
Else
c = dgv.DefaultCellStyle.BackColor
End If
e.CellStyle.BackColor = c
End If
End If
End If

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100