Solved

How to set different colours of font in C1TrueDGGrid when condition different?

Posted on 2011-03-06
11
460 Views
Last Modified: 2012-05-11
Hi, Experts

I would like  to set difference colours of font when in datagrid data as below:-

if Date < TodayDate then set font = Red
if Date = TodayDate then set font = Black
if Date > TodayDate then set font = Blue

Below is the code that i used but still not accurate:-

        Dim N As String
        Dim Day As String = Trim(txtToday.Text)
       
            N = Val(Me.grdSProcess.Columns("StartDate").CellText(e.Row))
            If N < Day Then
                e.CellStyle.ForeColor = System.Drawing.Color.Red
            ElseIf N > Day Then
                e.CellStyle.ForeColor = System.Drawing.Color.Black
            End If
     
Thanks
0
Comment
Question by:chkueh
  • 6
  • 5
11 Comments
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35055526
you can use OnItemDataBound event : this event is fired every time an item is added to datagrid, during page creation.


in aspx code add OnItemDataBound="Item_Bound" in datagrid tag :

 <asp:DataGrid id="C1TrueDGGrid" runat="server" OnItemDataBound="Item_Bound" ... and so on

and in codebehind  :

Sub Item_Bound(sender As Object, e As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.Item OR e.Item.ItemType =  ListItemType.AlternatingItem Then

Dim Day As String = Trim(txtToday.Text)
       
            N = Val(e.Item.Cells[3].text)  'for example, with startdate in column n°3

            If N < Day Then
                e.Item.Cells[3].ForeColor  = System.Drawing.Color.Red
            ElseIf N > Day Then
                e.Item.Cells[3].ForeColor  = System.Drawing.Color.Black
            End If
end if

end sub    

 
0
 

Author Comment

by:chkueh
ID: 35204234
Hi, Tuyau2poil

any idea to write the code in vb.net?

I had try using string as below code then it's works and in the Grid properties i had set FetchRowStyle=True :-

Private Sub grdC1_FetchRowStyle(ByVal sender As Object, ByVal e As C1.Win.C1TrueDBGrid.FetchRowStyleEventArgs) Handles grdC1.FetchRowStyle
       
        Dim K As String = (Me.grdC1.Columns("Quantity 01").CellText(e.Row))
        Dim N As String = (Me.grdC1.Columns("Quantity 02").CellText(e.Row))

        If K = N Then
            e.CellStyle.ForeColor = System.Drawing.Color.Black
        Else
            e.CellStyle.ForeColor = System.Drawing.Color.Red
        End If

    End Sub

But for datetime...currently it is not accurate, and i need 3 conditions as below:-

if Date < TodayDate then set font = Red
if Date = TodayDate then set font = Black
if Date > TodayDate then set font = Blue

Thanks in advance...
0
 
LVL 4

Assisted Solution

by:Tuyau2poil
Tuyau2poil earned 500 total points
ID: 35204847
If I understand right :

'get date string
Dim StrDate  As String = (Me.grdC1.Columns("StartDate").CellText(e.Row))

'convert to date type
RealDate=cdate(Strdate)

'use dateDiff function to compare to now
if DateDiff(DateInterval.Day, Now,RealDate) < 0 then set font = Red
if DateDiff(DateInterval.Day, Now,RealDate) = 0 then set font = Black
if DateDiff(DateInterval.Day, Now,RealDate) > 0 then set font = Blue



 
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35204856
obviously replace 'set font = Red' with  'e.CellStyle.ForeColor = System.Drawing.Color.Red' :-)

0
 

Author Comment

by:chkueh
ID: 35364457
Hi, Tuyau2poil

How i need to declare this "RealDate=cdate(Strdate)", because i declare as datetime also not correct? The error message is "Module 'DateAndTime' cannot be used as a type".

Thanks

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 500 total points
ID: 35364544
dim RealDate as Date

0
 

Assisted Solution

by:chkueh
chkueh earned 0 total points
ID: 35366782
Hi, Tuyau2poil...

Thanks for your codes.

My problem solved by using below code:-
 Dim StrDate As String = (Me.grdC1e.Columns("StartDate").CellText(e.Row))

        If StrDate < Date.Today Then

            e.CellStyle.ForeColor = System.Drawing.Color.Red()

        ElseIf StrDate = Date.Today Then

            e.CellStyle.ForeColor = System.Drawing.Color.Black()

        ElseIf StrDate > Date.Today Then

            e.CellStyle.ForeColor = System.Drawing.Color.Green() '.Blue()

        End If

Thanks for your help..
0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35367430
Your Code is a bit tricky Chkueh... you compare a string to a date...
( StrDate < Date.Today )

Normal way is to convert string to date before :

RealDate=cdate(Strdate)
if realdate < now then... etc...

but in this case, difference can be one minute !
so to specify a minimal one-day difference you must write :

if day(realdate) < day(now) then...
or
if DateDiff(DateInterval.Day, Now,RealDate) < 0 then set font = Red

rgds.
0
 

Author Comment

by:chkueh
ID: 35371442
Hi, Tuyau2poil

Can you explain the usage of---- if DateDiff(DateInterval.Day, Now,RealDate) < 0---

Why we use "< 0"? What does it means?

May be in future the code that i use having problem then i can use your code.

Thanks


0
 
LVL 4

Expert Comment

by:Tuyau2poil
ID: 35372324

datediff return long value equal to date2 - date1, with interval unit specified :
X= datediff (unit, date1, date2)

some examples : (dates are day/month/year HH:MM:SS)

        TextBox1.Text = DateDiff(DateInterval.Day, CDate("11/04/2011 10:00:00"), CDate("12/04/2011 10:00:00"))
        TextBox2.Text = DateDiff(DateInterval.Day, CDate("11/04/2011 10:00:00"), CDate("12/04/2011 09:00:00"))
        TextBox3.Text = DateDiff(DateInterval.Day, CDate("11/04/2011 09:00:00"), CDate("12/04/2011 10:00:00"))
        TextBox4.Text = DateDiff(DateInterval.Day, CDate("11/04/2011"), CDate("12/04/2011"))
        TextBox5.Text = DateDiff(DateInterval.Day, CDate("11/04/2011"), CDate("13/04/2011"))
        TextBox6.Text = DateDiff(DateInterval.Hour, CDate("11/04/2011 11:00:00"), CDate("12/04/2011 10:00:00"))

result :

        TextBox1.Text = 1
        TextBox2.Text = 0 (23h00 so less than one day)
        TextBox3.Text = 1 (25h00 so more than one day but less than two)
        TextBox4.Text = 1 (no time, so system put default 00:00:00 time)
        TextBox5.Text = 2
        TextBox6.Text = 23 (unit changed to hour)

good code !
0
 

Author Closing Comment

by:chkueh
ID: 35406617
Problem Solved..Thanks for the help...the is the website that i use

http://www.ehow.com/how_8142484_compare-date-vbnet.html
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now