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

Custom validators and autogenerated edit form

3 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kathy
Top achievements
Rank 1
Kathy asked on 06 Oct 2010, 10:43 PM
In the examples, all I see are edit item templates and validation on those fields in the template.  What about if you are using the autogenerated edit form?  Any examples like that?  Thanks.

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 07 Oct 2010, 05:40 AM
Hello Kathy,


The documentation shows how to add the validation controls in editform of RadGrid. I hope this helps.
Validation



-Shinu.
0
Kathy
Top achievements
Rank 1
answered on 08 Oct 2010, 09:05 PM
I got the validators to work for the numeric columns.  But I'm having difficulty with the DateTimeColumn when using a DatePicker.  I have 2 date columns, start date and end date and all I need to do is make sure the end date is after the start date.  Here's what I have:
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
  
                GridEditableItem editedItem = e.Item as GridEditableItem;
  
  
                GridDateTimeColumnEditor editor = (GridDateTimeColumnEditor)editedItem.EditManager.GetColumnEditor("StartDate");
                RadDateInput cell = (RadDateInput)editor.PickerControl.DateInput;
                cell.ID = "startdateID";
  
                GridDateTimeColumnEditor editorend = (GridDateTimeColumnEditor)editedItem.EditManager.GetColumnEditor("EndDate");
                RadDateInput cellend = (RadDateInput)editorend.PickerControl.DateInput;
                cellend.ID = "enddateID";
  
  
  
                CompareValidator compValidate = new CompareValidator();
                compValidate.ControlToValidate = cell.ID;
                compValidate.ControlToCompare = cellend.ID;
                compValidate.Operator = ValidationCompareOperator.GreaterThan;
                compValidate.ErrorMessage = "EndDate should not be less than StartDate";
                cell.Controls.Add(compValidate);
                cellend.Controls.Add(compValidate);
But now, it says that it can't find the controls enddateID and startedateID.
0
Vasil
Telerik team
answered on 14 Oct 2010, 12:14 PM
Hello Kathy,

This code should work for you:

protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
 
            GridDateTimeColumnEditor editorStartDate = (GridDateTimeColumnEditor)item.EditManager.GetColumnEditor("StartDate");
            GridDateTimeColumnEditor editorEndDate = (GridDateTimeColumnEditor)item.EditManager.GetColumnEditor("EndDate");
 
            TableCell cellStart = (TableCell)editorStartDate.PickerControl.Parent;
            CompareValidator compareValidator = new CompareValidator();
            compareValidator.ID = "compareValidator";
            compareValidator.ControlToValidate = editorStartDate.PickerControl.ID;
            compareValidator.ControlToCompare = editorEndDate.PickerControl.ID;
            compareValidator.Operator = ValidationCompareOperator.LessThan;
            compareValidator.Type = ValidationDataType.Date;
            compareValidator.ErrorMessage = "Start date must be before the end date!";
            cellStart.Controls.Add(compareValidator);
        }
    }

Information about how to add validators to RadGrid columns can be found in this help article and in the live demo here.

Sincerely yours,
Vasil
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Kathy
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Kathy
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or