Click here to Skip to main content
14,333,124 members
Rate this:
Please Sign up or sign in to 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
Rate this:
Please Sign up or sign in to vote.

Solution 1

Something like this:
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)[^]
   
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?
RickZeeland 21-Aug-17 8:51am
   
Yes, I updated the solution in answer to your question, I also sent a reply. Didn't you see that ?
Mohammad Imran1 21-Aug-17 8:58am
   
No i didn't see..below code is not working..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'
{

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;
}
}

RickZeeland 21-Aug-17 9:23am
   
It's no use repeating the same question, the answer which I gave in the solution above is:
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.
Mohammad Imran1 21-Aug-17 9:29am
   
Sorry i tried but still the same. Let me tell you that i have CLOCK IN column in database and data type is time.

Kindly let me know i'm new one..i don't know how to part time from this query.

Waiting
Thanks
Mohammad Imran1 21-Aug-17 9:22am
   
Remember i have Clock In column data type time in database
RickZeeland 21-Aug-17 9:26am
   
That's not a good idea, you should store Date + Time in DateTime.
Mohammad Imran1 21-Aug-17 9:30am
   
Ok boss..Let me check with that
Mohammad Imran1 22-Aug-17 1:03am
   
Not working at all.. after changed data type 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;

}
}
}
Rate this:
Please Sign up or sign in to vote.

Solution 3

When working with Time fields, it would be:
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.
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 5

I want to save file in folder instead of database Kindly help me..
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100