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

CompareValidator for less than equal

6 Answers 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ida
Top achievements
Rank 1
Ida asked on 11 Dec 2013, 09:59 AM
Hi,
Could anyone advise me please as to how to add addtional comparsion validation to my radgrid below.

I have 2 columns that are required fields: BasketsCaughtTotal and BasketsSampledTotal

I also require that BasketsCaughtTotal must always be equal to or greater than BasketsSampledTotal

<Columns>
                     <telerik:GridBoundColumn DataField="TripCode" FilterControlAltText="Filter TripCode column" HeaderText="Trip Code" SortExpression="TripCode" UniqueName="TripCode" ReadOnly="true"></telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="VesselCode" HeaderText="Vessel" UniqueName="VesselCode" Visible="false" ReadOnly="true"></telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="HaulNumber" HeaderText="Haul" UniqueName="HaulNumber" ReadOnly="true"></telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="RigNumber" HeaderText="Rig"  UniqueName="RigNumber" ReadOnly="true"></telerik:GridBoundColumn>    
                     <telerik:GridBoundColumn DataField="CatchTypeName" HeaderText="Catch Type" UniqueName="CatchTypeName" ReadOnly="true"></telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="CatchTypeCode" HeaderText="Catch Type Code" Visible="false" UniqueName="CatchTypeCode" ReadOnly="true"></telerik:GridBoundColumn>
                     <telerik:GridCalculatedColumn HeaderText="Raising Factor" UniqueName="RaisingFactor" DataType="System.Decimal" DataFormatString="{0:N2}" DataFields="BasketsCaughtTotal, BasketsSampledTotal" Expression="{0}/{1}" />
                     <telerik:GridBoundColumn DataField="BasketsCaughtTotal" HeaderText="Baskets Caught Total" UniqueName="BasketsCaughtTotal" ColumnEditorID="BasketsCaughtTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                          <ColumnValidationSettings EnableRequiredFieldValidation="True">
                               <RequiredFieldValidator ID="RFTotalBasketsCaught" Font-Bold="True"> Enter baskets caught</RequiredFieldValidator>
                           </ColumnValidationSettings>
                     </telerik:GridBoundColumn>
                     <telerik:GridBoundColumn DataField="BasketsSampledTotal" HeaderText="Baskets Sampled Total" UniqueName="BasketsSampledTotal" ColumnEditorID="BasketsSampledTotal" Visible="false" DataType="System.Decimal" DataFormatString="{0:N5}">
                          <ColumnValidationSettings EnableRequiredFieldValidation="True">
                               <RequiredFieldValidator ID="RFTotalBasketsSampled" Font-Bold="True"> Enter baskets sampled</RequiredFieldValidator>
                           </ColumnValidationSettings>
                     </telerik:GridBoundColumn>
                    </Columns>

Thank you, Ida

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Dec 2013, 11:32 AM
Hi Ida,

You can add the CompareValidator in the ItemCreated event as follows:

C#:
protected void RadGrid1_ItemCreated1(object sender, GridItemEventArgs e)
{
   if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        TextBox BasketsCaughtTotal = (TextBox)item["BasketsCaughtTotal"].Controls[0];
        TextBox BasketsSampledTotal = (TextBox)item["BasketsSampledTotal"].Controls[0];
        TableCell cell = (TableCell)BasketsCaughtTotal.Parent;
        CompareValidator val = new CompareValidator();
        val.ControlToValidate = BasketsCaughtTotal.ID;
        val.ControlToCompare = BasketsSampledTotal.ID;
        val.Operator = ValidationCompareOperator.GreaterThanEqual;
        val.ErrorMessage = "Should be Greater than or Equal to BasketsSampledTotal";
        val.ForeColor = Color.Red;
        cell.Controls.Add(val);
    }            
}

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 11 Dec 2013, 12:00 PM
Thank you Princy. Would it be ok to have this code in VB.Net?

My apologises - I should have specified I required code to be in VB.Net.

Thank you, Ida
0
Princy
Top achievements
Rank 2
answered on 11 Dec 2013, 12:12 PM
Hi Ida,

Here is the code in VB. You can use this Code-Converter to covert to VB and vice-verse.

VB:
Protected Sub RadGrid1_ItemCreated1(sender As Object, e As GridItemEventArgs)
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
        Dim BasketsCaughtTotal As TextBox = DirectCast(item("BasketsCaughtTotal").Controls(0), TextBox)
        Dim BasketsSampledTotal As TextBox = DirectCast(item("BasketsSampledTotal").Controls(0), TextBox)
        Dim cell As TableCell = DirectCast(BasketsCaughtTotal.Parent, TableCell)
        Dim val As New CompareValidator()
        val.ControlToValidate = BasketsCaughtTotal.ID
        val.ControlToCompare = BasketsSampledTotal.ID
        val.[Operator] = ValidationCompareOperator.GreaterThanEqual
        val.ErrorMessage = "Should be Greater than or Equal to BasketsSampledTotal"
        val.ForeColor = Color.Red
        cell.Controls.Add(val)
    End If
End Sub

Thanks,
Princy
0
Ida
Top achievements
Rank 1
answered on 11 Dec 2013, 01:56 PM
Many thanks Princy! That worked!
0
Yueming
Top achievements
Rank 1
answered on 25 Aug 2016, 03:46 PM

I have two GridDateTimeColumn in RadGrid1

<telerik:GridDateTimeColumn DataField="FromDate" DataFormatString="{0:d}" FilterControlWidth="98px" HeaderButtonType="TextButton" HeaderStyle-Width="125" HeaderText="From Date ">
<ColumnValidationSettings EnableModelErrorMessageValidation="true" EnableRequiredFieldValidation="true">
<RequiredFieldValidator ErrorMessage="* required" ForeColor="Red"></RequiredFieldValidator>
<ModelErrorMessage BackColor="Red" />
</ColumnValidationSettings>
</telerik:GridDateTimeColumn>
<telerik:GridDateTimeColumn DataField="ToDate" DataFormatString="{0:d}" FilterControlWidth="98px" HeaderButtonType="TextButton" HeaderStyle-Width="125" HeaderText="To Date">
<ColumnValidationSettings EnableModelErrorMessageValidation="true" EnableRequiredFieldValidation="true">
<RequiredFieldValidator ErrorMessage="* required" ForeColor="Red"></RequiredFieldValidator>
<ModelErrorMessage BackColor="Red" />
</ColumnValidationSettings>
</telerik:GridDateTimeColumn>

 

And I want to add compareValidator in RadGrid1_ItemCreated. Like this

 

Private Sub RadGrid1_ItemCreated1(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemCreated
If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then

Dim fromDatecolumnEditor As GridDateTimeColumnEditor = TryCast(DirectCast(e.Item, GridEditableItem).EditManager.GetColumnEditor("FromDate"), GridDateTimeColumnEditor)
Dim toDatecolumnEditor As GridDateTimeColumnEditor = TryCast(DirectCast(e.Item, GridEditableItem).EditManager.GetColumnEditor("ToDate"), GridDateTimeColumnEditor)
Dim datecell As TableCell = DirectCast(toDatecolumnEditor.PickerControl.Parent, TableCell)
Dim compareValidator As New CompareValidator()

compareValidator.ID = "compareValidator"
compareValidator.ControlToValidate = toDatecolumnEditor.PickerControl.ID
compareValidator.ValueToCompare = fromDatecolumnEditor.PickerControl.ID
compareValidator.[Operator] = ValidationCompareOperator.GreaterThanEqual
compareValidator.Type = ValidationDataType.[Date]
compareValidator.ForeColor = Drawing.Color.Red
compareValidator.ErrorMessage = "To date must be later than from date."
datecell.Controls.Add(compareValidator)

End If

But, it is not work. Please help.

It works if make change to

'compareValidator.ValueToCompare = DateTime.Now.ToShortDateString

0
Eyup
Telerik team
answered on 30 Aug 2016, 10:47 AM
Hi Yueming,

Generally, we suggest that you use the built-in range filtering of GridDateTimeColumn as stated in the following post:
http://marketplace.telerik.com/forums/filtering-issues-f154584cbb0b#K4cEr7lxV0O0GYt4ib3GrA

Alternatively, you can check the RadFilter control if you want to provide the filtering capability outside the grid:
http://demos.telerik.com/aspnet-ajax/filter/examples/integration/defaultcs.aspx?product=filter


I hope this will prove helpful.

Regards,
Eyup
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
Grid
Asked by
Ida
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ida
Top achievements
Rank 1
Yueming
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or