Solved

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

Posted on 2011-03-06
11
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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