Solved

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

Posted on 2011-03-06
11
462 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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