blah blah blah is here! blah blah » Close

up1down
link

Hi Guys, I have a datagrid that on load the below code checks through the date column and if any date is older than today it changes Cell [6] value to "1"

I want to do the exact function but only if the date is today at 10:00am, could i use something like below?:

foreach (DataGridViewRow row in salesDataGridView1.Rows)
{
if (row.IsNewRow) break;
DateTime dt = DateTime.Parse(row.Cells[1].Value.ToString());
if (dt < DateTime.Today "dd/MM/yyyy 10:00)
{
row.Cells[6].Value = "1";
}
}

last answered 7 months ago

2 answers

link

I'd use:

if (dt < DateTime.Today.AddHours(10))

Nice one vulpes, that's perfect - I'm inserting the date time in the grid in this format: DateTime.Now.ToString("dd/MM/yyyy HH:mm"); will that store it as a 24hr clock format ok?

vulpes
12813

That's right, using HH rather than hh will store the date in 24 hour format.

up0down
link

Vulpes going on from the code above, i would also like to change a value in the datagrid cell 16 hours behind the time now is it possible? i've tried the below but it doesn't seem to work?

if (row.IsNewRow) break;
DateTime dt = DateTime.Parse(row.Cells[1].Value.ToString());

if (dt < DateTime.Now.AddHours(-16))
{
row.Cells[6].Value = "0";
}

vulpes
12813

Well, I'd expect that to work though now you're doing it relative to DateTime.Now rather than DateTime.Today it could, of course, change the cells affected from instant to instant rather than just once a day. Can you give an example where it doesn't appear to work?

As an example it ignores -16 hours and changes entries even 2 days ago, so for instance every cell [6] that is over 1 day old gets a value off "1" what i want to do is check the date in the cell and if it's 16 hours behind datetime.now change the valu to "0"

Ahh, i just seemed to have got it working by changing the less than round to greater than so the line now reads; if (dt >= DateTime.Now.AddHours(-16)) this now only changes entries that are 16 hours behind.

Feedback