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

Private
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()
  Try
 
    ' 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
        e.DrawBackground()
      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)
 
      Return
 
    End If
 
    ' Draw normal text for a subitem with a nonnegative
    ' or nonnumerical value.
    e.DrawText(flags)
 
  Finally
    sf.Dispose()
  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

else

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
HI

Here is something that you can try

The gridview

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

Code Behind

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

   
Dim d As New DataTable
    d
.Columns.Add("T1")
    d
.Columns.Add("T2")
    d
.Rows.Add(1, 2)

    grdv
.DataSource = d
    grdv
.DataBind()

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.

refer
http://forums.asp.net/t/1663363.aspx/2/10?RE+Change+Gridview+row+value+color+based+on+current+date
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)
        e.DrawFocusRectangle()

    Else

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

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

End Sub

refer this for more details
http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem.aspx
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!