Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey Guys, I have a datagridview in winforms that has the following columns:

MileDate    begin_Miles       end_Miles       car_ID
02/15/2022      200              206             1
02/16/2022      206          Value to Check      1


Value to check should always be greater than begin_Miles. Here is the code, but nothing happens when I enter end_Miles less than begin_Miles.

What I have tried:

Cell Validating

C#
private void dataGridView1_ValidatingEditor(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex > 1)
            {
                if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[2].EditedFormattedValue.ToString()) && !string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[1].EditedFormattedValue.ToString()))
                {
                    int minMiles = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[1].EditedFormattedValue);
                    int obtainMiles = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[2].EditedFormattedValue);
                    if (obtainMiles <= minMiles)
                    {
                        MessageBox.Show("End Miles should always be greater than Begin Miles");
                    }
                }
            }
        }
Posted
Updated 23-Mar-22 4:28am
v2

As MSDN documentation[^] states if the datagridview row is new row, then don't try to validate it until finishing editing.
Try to use: DataGridView.CellEndEdit Event (System.Windows.Forms) | Microsoft Docs[^]

BTW: you use wrong format of date!
If you want to avoid problems in date time comparison use date in ISO format. See: SQLite.org: Date And Time Functions[^]

Available formats are:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
 
Share this answer
 
For future reference:

Here was what finally worked for me:

C#
private void dataGridView1_ValidatingEditor(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex > 1)
            {
                if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["end_Miles"].EditedFormattedValue.ToString()) && !string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["begin_Miles"].EditedFormattedValue.ToString()))
                {
                    int minMiles = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells["begin_Miles"].EditedFormattedValue);
                    int obtainMiles = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells["end_Miles"].EditedFormattedValue);
                    if (obtainMiles <= minMiles)
                    {
                        MessageBox.Show("End Miles should always be greater than Begin Miles");
                    }
                }
            }
        }
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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