Hi
What wrong with my code
I'm using datagridview with C#. There are two columns in my grid (day1 and day2 bounddata). I want to validate day 2 not smaller than
day 1. The problem is that I can't the the latest value of day 2 column. Day 2
always return the old value of it. Help me pls.
Thank you so much.
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 2)
{
DateTime day1 = (DateTime) dataGridView1.Rows[e.RowIndex].Cells[2].Value;
DateTime day2 = (DateTime) dataGridView1.Rows[e.RowIndex].Cells[3].Value;
if (day2 < day1)
{
MessageBox.Show("Day 1 not smaller
than day 2");
}
}
}

Master BillaRank #2,229 · 630 reputationPosted Sep 10, 2009, 2:34 AM EDT
Just simple
Hi
just do a small change
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 2)
{
DateTime day1 = (DateTime) e.FormattedValue;
DateTime day2 = (DateTime) dataGridView1.Rows[e.RowIndex].Cells[3].Value;
if (day2 < day1)
{
MessageBox.Show("Day 1 not smaller than day 2");
}
}
}
Thank youQuoc Duong An6 reputationPosted Sep 11, 2009, 12:01 AM EDT
Thanks for your reply. I try your recommend like this
DateTime ngay1 = (DateTime)e.FormattedValue;
But the error occurs 'Specified cast is not valid' after I type datetime value. I don't know why. By the way I use 'CalendarColumn' of MS sample for day1, day2 columns.
Thanks