C# - Compare Date Column from DataGrid to Today's Date
-
Tuesday, July 01, 2008 12:13 AM
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 with todays date and all records that are expired, 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.
TBone
All Replies
-
Tuesday, July 01, 2008 8:01 AM
I will give you an example:
DataTable dt = new DataTable("TestTable");
dt.Columns.Add("Column1", typeof(int));
dt.Columns.Add("Column2", typeof(string));dt.Rows.Add(1, "kira");
dt.Rows.Add(2, "qian");
dt.Rows.Add(3, "kira qian");this.dataGridView1.DataSource = dt;
then hold the DataBindingComplete Event of the dataGridView1 and code
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
{
DataGridViewRow dgvr = dataGridView1.Rows
;
if (dgvr.Cells[1].Value.ToString() == "kira")
{
dgvr.DefaultCellStyle.BackColor = Color.Red;
}
}
}this example color the row of the whose column2 is == "kira" and give a red color
I think you can refer it for your date compace
-
Tuesday, July 01, 2008 8:05 AM
sorry, the dataGridView1.Rows[ i ] has been convert to a light icon by the forum automatically,
-
Tuesday, July 01, 2008 2:01 PMokay...but how would you compare the dates? The expire_date column that has a stored value from the database and today's date (DataTime.Now.Date).
-
Wednesday, July 02, 2008 1:00 AM
Use timeSpan to define the yesterday
System.TimeSpan timeSpan = new TimeSpan(1, 0, 0, 0); // the number 1 means 1 day span, you can also set the
//value to meet your time span needs.
System.DateTime yesterday = System.DateTime.Now.Date - timeSpan;if(yesterday > expire_date_Column) // the expire_date_Column is earlier than yesterday, than you can know it is expired
{
...... // set the row color
}
-
Wednesday, July 02, 2008 2:36 AM
May be the static method compare() of DateTime can help you. You can use it as
ateTime.compare(DateTime dt1,DateTime dt2) . It returns a int value. If the return value is big than zero then dt1>dt2, Else dt1<=dt2. -
Tuesday, July 22, 2008 12:00 AM
Based on what everyone said and some more research, this is what I did to get it to work.
foreach
(DataGridViewRow dr in datagrid1.Rows){
if (dr.Cells["DueColumn"].Value.ToString() != ""){
DateTime date = DateTime.Now.Date; DateTime expiredate = ((DateTime)dr.Cells["DueColumn"].Value); if (expiredate < date){
dr.DefaultCellStyle.ForeColor =
Color.Red; Font font = new Font(datagrid1.DefaultCellStyle.Font.FontFamily, 8, FontStyle.Bold);dr.DefaultCellStyle.Font = font;
}
}
}
Thanks for your help everyone.
TBone


