This is a migrated thread and some comments may be shown as answers.

[Solved] Add Validator to GridDateTimeColumn

1 Answer 224 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Markus
Top achievements
Rank 1
Markus asked on 24 May 2008, 09:55 AM

I just tried to figure out how to add a RequiredFieldValidator to an GridDateTimeColumn.

I tried the code from this example in the documentation, but it didn't work for the DateTimeColumn because editor.TextBoxControl is always null. Instead you have to get a reference to the PickerControl:

void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)  
{  
    if (e.Item.IsInEditMode && e.Item is GridEditableItem)  
    {  
        GridEditableItem item = e.Item as GridEditableItem;  
 
        GridDateTimeColumnEditor dateEditor = item.EditManager.GetColumnEditor("StartDate"as GridDateTimeColumnEditor;  
        // Adding a validator to the DateTimeColumn. This one works  
        if (dateEditor != null && dateEditor.PickerControl != null)  
        {  
            TableCell cell = dateEditor.PickerControl.Parent as TableCell;  
 
            //Don't set the id here. Otherwise you'll get an exception  
            //dateEditor.PickerControl.ID = "StartDate_ID_Validation";  
 
            RequiredFieldValidator validator = new RequiredFieldValidator();  
            validator.ControlToValidate = dateEditor.PickerControl.ID;  
            validator.ErrorMessage = "StartDate required";  
            cell.Controls.Add(validator);  
        }  
    }  

I don't know if this is the desired behaviour or a bug (I mean TextBoxControl being null), but it works fine.

Best regards
Sebastian

1 Answer, 1 is accepted

Sort by
0
Missing User
answered on 27 May 2008, 10:32 AM
Hello Sebastian,

You can use the same approach as described in the help article. You simply have to use PickerControl instead of TextBoxControl property


All the best,
Plamen
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
Markus
Top achievements
Rank 1
Answers by
Missing User
Share this question
or