Completed
Last Updated: 12 Nov 2013 08:54 by ADMIN
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 12 Nov 2013 08:54
Category: Editors
Type: Bug Report
0
FIX. RadGridView - GridViewDateTimeColumn does not allow entering a year between MinValue and MaxValue
To reproduce:
-add RadGridView and use the following code:

public static readonly DateTime MIN_DATE = new DateTime(1900, 1, 1);
public static readonly DateTime MAX_DATE = new DateTime(2079, 1, 1);

public Form1()
{
    InitializeComponent();

    var colDate = new Telerik.WinControls.UI.GridViewDateTimeColumn();
    colDate.DataType = typeof(System.DateTime);
    colDate.HeaderText = "Date";
    colDate.Name = "colDate";
    colDate.FormatString = "{0:d}";
    colDate.Width = 85;

    radGridView1.Columns.Add(colDate);

    radGridView1.CellEditorInitialized += radGridView1_CellEditorInitialized;
}

private void radGridView1_CellEditorInitialized(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e)
{
    var editor = this.radGridView1.ActiveEditor as RadDateTimeEditor;
    if (editor == null)
    {
        return;
    }
    editor.MinValue = MIN_DATE;
    editor.MaxValue = MAX_DATE;

    DateTime date = DateTime.Now;
    RadDateTimeEditorElement editorElement = (RadDateTimeEditorElement)editor.EditorElement;
    editorElement.Format = DateTimePickerFormat.Custom;
    editorElement.CustomFormat = "MM/dd/yyyy";
    editorElement.Value = date;
    e.Row.Cells[e.ColumnIndex].Value = editorElement.Value;
}

As a result when the user tries to enter a valid year between MinValue and MaxValue it is not possible.

Workaround: use CellValidating event:

radGridView1.CellValidating += radGridView1_CellValidating;

 private void radGridView1_CellValidating(object sender, CellValidatingEventArgs e)
 {
     RadGridView grid = sender as RadGridView;
     if (grid.CurrentColumn is GridViewDateTimeColumn)
     {
         DateTime currentDate = (DateTime)e.Value;
         if (currentDate <= MAX_DATE && currentDate >= MIN_DATE)
         {
             e.Cancel = false;
         }
         else
         {
             e.Cancel = true;
         }
     }
 }
0 comments