6 Replies - 5332 Views - Last Post: 09 February 2015 - 05:48 AM Rate Topic: -----

#1 mymbong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-January 15

Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 04:14 AM

please help i have dgv populated from ms access db

one of the column name is "date"

i want to color based on the date value

for example

2/1/2015 To 2/10/2015 is color yellow
2/11/2015 To 2/20/2015 is color orange
2/21/2015 To 2/28/2015 is color pink

i want every 10 days of every month

this is first try

Private Sub dgvwithdrawed_RowPrePaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles dgvwithdrawed.RowPrePaint


        For Each row As DataGridViewRow In dgvwithdrawed.Rows
            Dim now = Date.Now
            Dim ndate = DateTime.Parse(row.Cells("DATE").Value.ToString)
            Dim tdays = now.AddDays(+10)

            If (now > ndate) Then
                row.DefaultCellStyle.BackColor = Color.Yellow

            End If
        Next


    End Sub



but not working please see the attached file

any help please

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Change color of datagridview row based on date value of cell

#2 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:07 AM

Hi

Maybe I have misunderstood, but do you actually need to do anything with the dates other than working out what day of the month the date coming in is. The way I see it, if it is 10 or less then it is yellow, greater than 10 but less than 21 then it is orange otherwise it is pink.

Something like:

    Private Sub dgvwithdrawed_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles dgvwithdrawed.RowPrePaint

        If dgvwithdrawed.Rows(e.RowIndex).Cells("DATE").Value IsNot Nothing Then

            Dim ndate = DateTime.Parse(dgvwithdrawed.Rows(e.RowIndex).Cells("DATE").Value.ToString)

            If ndate.Day <= 10 Then
                dgvwithdrawed.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow
            ElseIf ndate.Day > 10 And ndate.Day <= 20 Then
                dgvwithdrawed.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Orange
            Else
                dgvwithdrawed.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Pink
            End If

        End If

    End Sub



Also, you will note in the above that you do not need the For Loop as the event will fire for each row added.


HTH
Was This Post Helpful? 0
  • +
  • -

#3 Minimalist   User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 240
  • Joined: 13-November 13

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:14 AM

Since you want to do it for every months the code could get complicated. Why not extract the day numbers and do the comparison just against the day numbers? I am working on a German system so my formatting is a bit different, this just shows how to get the daynumber:
 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim now As DateTime = DateTime.Now
        Dim str As String = ""
        Dim format As String = "d"
        str = now.ToString(format)
        'Dim stra() As String = str.Split("."c)
        Dim stra() As String = str.Split("/"c)
        'Debug.Print(stra(0))
        Debug.Print(stra(1))
    End Sub

This should work for your time format.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6696
  • View blog
  • Posts: 27,485
  • Joined: 12-December 12

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:28 AM

@OP You do not have to struggle to quote a previous post, there is a Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#5 mymbong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-January 15

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:37 AM

hi djjeavons thanks for your quick response

i try your code snippet but still not working

my first try actually im not sure to fit to my need

all i want is if dgv have date value from date 1 to 10 every month set color yellow,
and then if dgv have date value from 11 to 20 every month set color orange, and then if dgv have date value from 21 to 31 every month set color pink

please help

i have mistake andrewsw

This post has been edited by andrewsw: 09 February 2015 - 05:36 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#6 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:42 AM

Hi

The code that I posted does exactly what you have stated. It takes the Day portion of the date that is being bound and if it is less than 10 will colour the row yellow, between 11 and 20 orange and anything else (i.e. greater than 20), pink.

Can you post the code that you are now using which is not working.
Was This Post Helpful? 1
  • +
  • -

#7 mymbong   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-January 15

Re: Change color of datagridview row based on date value of cell

Posted 09 February 2015 - 05:48 AM

View Postdjjeavons, on 09 February 2015 - 05:42 AM, said:

Hi

The code that I posted does exactly what you have stated. It takes the Day portion of the date that is being bound and if it is less than 10 will colour the row yellow, between 11 and 20 orange and anything else (i.e. greater than 20), pink.

Can you post the code that you are now using which is not working.




hi djjeavons thank you so much for your code snippet its now working

i delete all existing file from debug folder
and now its working
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1