473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C# - Compare Date Column from DataGrid to Today's Date

I am new to C# and I am working on a Windows Form. I have a DataGrid that is getting data from an Oracle Database. I am trying to compare the "ExpireDate" column from my database with todays date. All records that are expired (from the ExpireDate column), change that row color.

Example: The code is wrong but I am trying to explain the concept.

if (ExpireDate > TodayDate)
DataGrid.Row.BackGroundColor = Red

I hope someone out there knows what to do. I am sure this is probably very easy, but I don't know what to do. I saw a lot of articles that are getting values through a double click event, but I don't want to do an event.

Thank you for your time in advance.

Mike
Jul 1 '08 #1
6 7402
nateraaaa
663 Expert 512MB
You need to utilize the OnItemDataBound event of the DataGrid. In this event you can loop through the rows of your datagrid and compare the cell text of the Expired column to today's date. If this comparison is true change the ForeColor property of the cell text.

Nathan
Jul 3 '08 #2
Cool, I am still new to C#. Do you mind giving me an example?
Thanks
Jul 7 '08 #3
nateraaaa
663 Expert 512MB
In the example below the Expiration Date column is the 1st column. This code will changed the backcolor of the Expiration Date cell only.

Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
  3. foreach(TableCell cell in e.Item.Cells)
  4. {
  5. if(e.Item.Cells.GetCellIndex(cell) == 1 && e.Item.DataSetIndex >= 0)
  6. {
  7. if(Convert.ToDateTime(cell.Text) > DateTime.Today)
  8. {
  9. cell.BackColor = System.Drawing.Color.Red;
  10. }
  11. }
  12. }
  13. }
  14.  
Nathan
Jul 9 '08 #4
Plater
7,872 Expert 4TB
Does DataGrid have that event? DataGridView does not. (GridView does though, I wish the other objects had it, it's great)
Jul 9 '08 #5
nateraaaa
663 Expert 512MB
Does DataGrid have that event? DataGridView does not. (GridView does though, I wish the other objects had it, it's great)
You can also use the OnPreRender event as an alernative to set properties for the datagrid before the DataGrid is displayed on the page.
Jul 9 '08 #6
Plater
7,872 Expert 4TB
You can also use the OnPreRender event as an alernative to set properties for the datagrid before the DataGrid is displayed on the page.
That too is only related to the GridView object and web-based objects.
The OP said they were on a windows form, so they either are using the DataGrid or the DataGridView object.
Jul 9 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: karunakar | last post by:
Hi All I was poplating Datagrid Datagrid has one of the column showing datetime field format iam showing DATE here not Necessary to populate the Time Frame Even my datebase column also...
4
by: yer darn tootin | last post by:
Does anyone know the sort expression for a column that's data has been returned in the format, eg '07 Jul 05'?? The sort expression {..:"dd mmm yy"} doesn't work ( if the column was returned as...
6
by: Agnes | last post by:
I understand it is impossible, but still curious to know "Can I freeze several column in the datagrid, the user can only scroll the first 3 columns (not verical), for the rest of the coulumn, it is...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
5
by: M Skabialka | last post by:
I am creating my first Visual Studio project, an inventory database. I have created a form and used written directions to add data from a table to the form using table adapters, data sets, etc. ...
0
by: Madeleine | last post by:
i want to check if the date selected is before the today date. if yes, i wish to return a message in the label. if (Convert.ToDateTime(Calendar1.SelectedDate.Date) =...
11
by: soni2926 | last post by:
Hi, I have a function where i need to check the date passed in is within 90days of today. Could someone help me get this to work, how do i do the 90 days compare with today? <script...
25
by: Brian | last post by:
I have a datetimepicker formated for just time, the user selects the time. I want to compare if that time is between midnight and 8 am dtmTime #11:59:59 PM# and dtmTime < #08:00:00 AM# this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.