Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,

1st , I have attendance database and i want to call in gridview and change color gridview column Name(ClockIn) if time exceed 9:30,

2nd, If time exceeded 9:30 then show on textbox OR Lable (Total Late) 1 and so on and on.

Pleas help i'm new one.

I have tried below code but its not working

DateTime.Parse(e.Row.Cells[5].Text) > DateTime.Parse("12:00:00 PM ")

What I have tried:

DateTime.Parse(e.Row.Cells[5].Text) > DateTime.Parse("12:00:00 PM ")
Posted
Updated 29-Mar-19 23:34pm

Something like this:
C#
private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		if (e.ColumnIndex == 5)
		{
			var mydatetime = DateTime.Parse(myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
			
			if (mydatetime.Hour > 9 && mydatetime.Minute > 30)
			{
				e.CellStyle.BackColor = Color.Yellow;
			}
		}
	}

Make sure the DateTime string in your DataGridView has the correct format, e.g.
"2008-05-01 7:34:42Z", you can omit the Time but not the Date part.
See: DateTime.Parse Method (System)[^]
 
Share this answer
 
v2
Comments
Mohammad Imran1 21-Aug-17 5:16am    
1, Error 1 The best overloaded method match for 'System.DateTime.Parse(string)' has some invalid arguments

2,Error 2 Argument 1: cannot convert from 'object' to 'string'
Mohammad Imran1 21-Aug-17 5:17am    
please also let me know below query i want to count total late if time exceeded 9:30

select ([Clock In]) from tbl_register where name = 'Kashif' and ([Clock In]) > '9:30'
RickZeeland 21-Aug-17 5:28am    
select COUNT([Clock In]) from tbl_register where name = 'Kashif' and ([Clock In]) > '9:30'
See: https://www.w3schools.com/sql/sql_count_avg_sum.asp
Mohammad Imran1 21-Aug-17 5:47am    
Ok Thanks... What about above query its giving error...
Error 1 The best overloaded method match for 'System.DateTime.Parse(string)' has some invalid arguments

2,Error 2 Argument 1: cannot convert from 'object' to 'string'
Mohammad Imran1 21-Aug-17 8:48am    
Hello any buddy is there?
When working with Time fields, it would be:
C#
private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		if (e.ColumnIndex == 5)
		{
			var mytime =myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
			
			if (mytime > "09:30")
			{
				e.CellStyle.BackColor = Color.Yellow;
			}
		}
	}
But you should do some checking first, if it is a valid Time and not NULL.
 
Share this answer
 
Comments
Mohammad Imran1 22-Aug-17 0:32am    
Its not working at all.. My time data type of column is datetime..
Mohammad Imran1 22-Aug-17 0:46am    
I have add below code ..but cell not change color.. my datatype is datetime..

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

if (e.ColumnIndex == 6)
{

var mydatetime = DateTime.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

if (mydatetime.Hour > 9 && mydatetime.Minute > 30)
{
e.CellStyle.BackColor = Color.Yellow;

}
}
}
RickZeeland 22-Aug-17 2:09am    
Run your code in Debug mode, place a breakpoint on the line var mydatetime, and see what the value is, step through your code using F10.
I want to save file in folder instead of database Kindly help me..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900