hey,

i am using a datagrid view calender column in a form, i add all the details of it to the database.(such as the due date as 08/20/2010) and then when i come and open it in the next day try to edit it, it gives an error saying "Argument Out of Range Exception was unhandled". how can i avoid this problem

thanx
i have attached a word document on the error.

Read the error text.

'Value' should be between 'MinDate' and 'MaxDate'

You set ctl.MinDate = System.DateTime.Now.Date so any cell with a date value before today will throw this error. What are you trying to acheive?
If you want to prevent the user from entering a value prior to the current date then use the CellValidating event to prevent it. That way your code can still load older dates.

Read the error text.


You set ctl.MinDate = System.DateTime.Now.Date so any cell with a date value before today will throw this error. What are you trying to acheive?
If you want to prevent the user from entering a value prior to the current date then use the CellValidating event to prevent it. That way your code can still load older dates.

what do you mean
how to do it for all the datagrid view calender column ??

thanx

When teh user finishes entering data in a cell the CellValidating event is called. Inside this event you need to check which column the user has edited. If its the calender column then check the date they entered. If it is valid then let them enter it, if it isn't valid then you can set the EventArgs Cancel property to true. If you set the Cancel to true then the cell wont validate and the user can't leave the cell without entering a valid value or cancelling the edit.

Heres a simple example which will prevent the user entering a blank string in column 2 of a datgridview. If the cell is empty then a message box informs the user of the mistake and the validation is cancelled:

private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (string.IsNullOrEmpty(e.FormattedValue.ToString()))
                {
                    MessageBox.Show("Please enter a value or press Esc to cancel", "Value required", MessageBoxButtons.OK);
                    e.Cancel = true;
                }
            }
        }

The EventArgs FormattedValue member contains the value the user has entered; it is this value you need to validate.

This article has been dead for over six months. Start a new discussion instead.