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");
}
}
}
Quoc Duong AnPosted Sep 11, 2009, 4:01 AM
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
Master BillaPosted Sep 10, 2009, 6:34 AM
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 you