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