CompareValidator for less than equal

7 posts, 0 answers
  1. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 11 Dec 2013 Link to this post

    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
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Dec 2013 Link to this post

    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
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 11 Dec 2013 Link to this post

    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
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Dec 2013 Link to this post

    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
  6. Ida
    Ida avatar
    55 posts
    Member since:
    Mar 2013

    Posted 11 Dec 2013 Link to this post

    Many thanks Princy! That worked!
  7. Yueming
    Yueming avatar
    10 posts
    Member since:
    Oct 2012

    Posted 25 Aug in reply to Princy Link to this post

    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

  8. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 30 Aug Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017