VB.NET - changing row color based on date - Asked By jenn on 12-Aug-11 04:39 PM

hi guys

gotta question i am hoping someone will answer for me.

gotta listview that is attached to textboxes and a datepicker

the datepicker is set to pick up the date of a scheduled task-

one of the other columns is labeled as "Next Due"-

this column is attached to a textbox where the user types in  a future date.

what i want to do is this:

if the future date has already past..i want that row to turn to a different color.

if someone can either give me an example or point me in the right direction for getting the result i would really appreciate it .

pete rainbow replied to jenn on 12-Aug-11 06:17 PM
change to ownerdraw style then hook to the DrawSubItem event

note you can check for column zero on details view which will do the whole row

or do it itme by item...

heres some sample code

Sub listView1_DrawSubItem(ByVal sender As Object, _
  ByVal e As DrawListViewSubItemEventArgs) _
  Handles listView1.DrawSubItem
  Dim flags As TextFormatFlags = TextFormatFlags.Left
  Dim sf As New StringFormat()
    ' Store the column text alignment, letting it default
    ' to Left if it has not been set to Center or Right.
    Select Case e.Header.TextAlign
      Case HorizontalAlignment.Center
        sf.Alignment = StringAlignment.Center
        flags = TextFormatFlags.HorizontalCenter
      Case HorizontalAlignment.Right
        sf.Alignment = StringAlignment.Far
        flags = TextFormatFlags.Right
    End Select
    ' Draw the text and background for a subitem with a
    ' negative value.
    Dim subItemValue As Double
    If e.ColumnIndex > 0 AndAlso _
      Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
      NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
      subItemValue < 0 Then
      ' Unless the item is selected, draw the standard
      ' background to make it stand out from the gradient.
      If (e.ItemState And ListViewItemStates.Selected) = 0 Then
      End If
      ' Draw the subitem text in red to highlight it.
      e.Graphics.DrawString(e.SubItem.Text, _
        Me.listView1.Font, Brushes.Red, e.Bounds, sf)
    End If
    ' Draw normal text for a subitem with a nonnegative
    ' or nonnumerical value.
  End Try
End Sub
jenn replied to pete rainbow on 12-Aug-11 07:07 PM
okay, maybe i am just missing the ball on this-

but what i am trying to do is not color rows- but this:

i have a listview with 9 columns.

on the form itself..i have a date time picker, this picker allows the user to pick what day he is scheduling his task for.

on the 9th column of my listview, it is a regular textbox- where the user types in a date of the next service call.

what i want to do is:

if the 9th column (service) date is dated - 10/10/2011

and todays date is - 10/20/2011--

then i want that particular row (the entire row) to turn to a different color (red)

so in other words, if the date the user typed in in the service column is past due- then turn that column red.

BUT- i so much can use these codes for cosmetic reasons for sure! :)
pete rainbow replied to jenn on 12-Aug-11 08:24 PM
it was meant as a starting point
simple enough to change and put in something along the lines of

if serviceDate is due then

do the new colouring


do nothing

end if

personally i find it better to be given a staring point rather than being spoon fed ;-)
Ravi S replied to jenn on 12-Aug-11 11:01 PM

Here is something that you can try

The gridview

    <asp:GridView runat="server" ID="grdv" AutoGenerateColumns="false">
<asp:TemplateField HeaderText="T1">
<%# Eval("T1")%>
<asp:TemplateField HeaderText="T2">
<%# Eval("T2")%>

Code Behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim d As New DataTable
.Rows.Add(1, 2)

.DataSource = d

End Sub

Private Sub grdv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdv.RowDataBound

Dim data As DataRowView = e.Row.DataItem
If data Is Nothing Then Exit Sub

If e.Row.RowType = DataControlRowType.DataRow Then

If data.Item("T1") <= data.Item("T2") Then e.Row.ForeColor = Color.Red

End If

End Sub

This should work for binding to a DataTable. If you're using a collection then the RowDataBound event will need changing slightly.

Anoop S replied to jenn on 13-Aug-11 12:41 AM
You will have to use custom drawing for that - see ListView.DrawItem event.
This event lets you customize the appearance of a ListView control using owner drawing. It is raised only when the OwnerDraw property is set to true. For more information about owner drawing, see the OwnerDraw property reference topic.

 Draws the backgrounds for entire ListView items.
Private Sub listView1_DrawItem(ByVal sender As Object, _
    ByVal e As DrawListViewItemEventArgs) _
    Handles listView1.DrawItem

    If Not (e.State And ListViewItemStates.Selected) = 0 Then

        ' Draw the background for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)


        ' Draw the background for an unselected item.
        Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
            Color.Maroon, LinearGradientMode.Horizontal)
            e.Graphics.FillRectangle(brush, e.Bounds)
        End Try

    End If
 ' Draw the item text for views other than the Details view.
    If Not Me.listView1.View = View.Details Then
    End If

End Sub

refer this for more details
dipa ahuja replied to jenn on 13-Aug-11 04:43 AM
Just add a textChange event , and where ever you are displaying the dates change the color of that date for ex:

Private Sub TextBox1_TextChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles TextBox1.TextChanged
    Dim dt As DateTime
    dt = Convert.ToDateTime(TextBox1.Text)
    Dim today As DateTime = DateTime.Now
    If (dt < today) Then
      MessageBox.Show("You have entered past date")
      'Write code to Change the color
    End If
  End Sub
jenn replied to jenn on 14-Aug-11 10:03 AM
ahhhhhhh! alot of this code block is looking familiar to me :)
me thinks i can take this example and manipulate it correctly! thank so much :)
jenn replied to dipa ahuja on 14-Aug-11 10:09 AM
yes, i really think this will work for me.
have not been doing this for long- and am in somewhat of uncharted territory- but alot of the example you used is covering alot of what i am familiar with, i think that it is a good starting point for me to be able to solve this issue and (lol) move onto the next one :)

thank hun!