Solved

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

Posted on 2011-03-06
11
461 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL syntax in VB.net 5 30
C# HTTP GET method sample code 3 42
Receiving a string from a WebService Push 21 38
C# guarantee sql connection close 6 32
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

25 Experts available now in Live!

Get 1:1 Help Now