Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-06
11
Medium Priority
?
467 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 2000 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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

Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

704 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