hi there,

this question have posted before also, but i ahave some more questions as well.
when i try to validate the datagridview cell which evet should i use in the datagrid view.

i and using the dgvActions_CellValidating event,but the thing is i want to validate the cell after the cell value has been selected.

the code i have written does the validation when i click on another cell in the datagrid view.
i have written the code to validate the datagridview cell 2 , but whe i clicl cell 3 only the validation event is triggered for cell 2.
why is this happening.

how can i make the validation trigger so that the value of the current cell is seelcted.

the code i wrote is below.

int row = dgvActions.SelectedCells[0].RowIndex;

            if (e.ColumnIndex == 2)
            {
                if (e.FormattedValue != null)
                {
                    
                    String TodayDate = System.DateTime.Now.Date.ToString();
                    int r = TodayDate.IndexOf(" ");
                    String date = TodayDate.Substring(0, r);

                    String Duedate = e.FormattedValue.ToString();

                    if (Duedate.CompareTo(date) < 0)
                    {
                        MessageBox.Show("invalid date");                        
                    }
                }
            }

thanxxxx

Recommended Answers

All 2 Replies

Validating events are used to help stop the user from leaving a cell with a bad value. The EventArgs usually has a Cancel property that if set to true will block the user from leaving the data entry field (cell, textbox, etc.). They are only triggered when the user tries to leave a field because only then can we be certain that the user has finished entering the value.
I recommend setting the Cancel property to true for invalid data as then the user focus will remain in cell 2. E.g. (inside your column test)

// holder for new value
DateTime Duedate;
// get newly entered value in local string
string DataEntry = e.FormattedValue as string;
// check if value is a valid DateTime and if its value >= today
if (!DateTime.TryParse(DataEntry, out Duedate) || (Duedate < DateTime.Today))
{
    // date invalid or out of range - prevent user leaving this cell
    e.Cancel = true;
    // inform user of error
    MessageBox.Show("invalid date");
}

BTW: This code looks like a long hand way to to get DateTime.Today;

String TodayDate = System.DateTime.Now.Date.ToString();
int r = TodayDate.IndexOf(" ");
String date = TodayDate.Substring(0, r);

Hope this helps:) If so, please remember to mark thread solved.

Validating events are used to help stop the user from leaving a cell with a bad value. The EventArgs usually has a Cancel property that if set to true will block the user from leaving the data entry field (cell, textbox, etc.). They are only triggered when the user tries to leave a field because only then can we be certain that the user has finished entering the value.
I recommend setting the Cancel property to true for invalid data as then the user focus will remain in cell 2. E.g. (inside your column test)

// holder for new value
DateTime Duedate;
// get newly entered value in local string
string DataEntry = e.FormattedValue as string;
// check if value is a valid DateTime and if its value >= today
if (!DateTime.TryParse(DataEntry, out Duedate) || (Duedate < DateTime.Today))
{
    // date invalid or out of range - prevent user leaving this cell
    e.Cancel = true;
    // inform user of error
    MessageBox.Show("invalid date");
}

BTW: This code looks like a long hand way to to get DateTime.Today;

String TodayDate = System.DateTime.Now.Date.ToString();
int r = TodayDate.IndexOf(" ");
String date = TodayDate.Substring(0, r);

Hope this helps:) If so, please remember to mark thread solved.

hey thankxx

but the thing is i have to click on another place in the datagrid view to trigger the event. so if i select invalid date i have to click another place in the datagrid view for the error msg to appear.how to i avoid this and make the event trigger after the cell value is beign selected?????


thankxxxxxxxxxx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.