Adding custom validator to gridnumeric column

7 posts, 0 answers
  1. NVB
    NVB avatar
    39 posts
    Member since:
    Apr 2009

    Posted 03 Jun 2012 Link to this post

    I tried to browse thru various form but I couldn't find how to add custom validator to GridNumeric column

    I need to have clientside validation and server side validation for gridnumeric columns

    I have 4 columns ( Grid Numeric columns) and total column and I need to make sure the total of this cannot be less than zero

    I tried to do this in  Item created event , but not successfull
    <telerik:GridNumericColumn DataField="AMT1" UniqueName="AMT1" HeaderText="Amount1"

    DataType="System.Int32" DataFormatString="{0:N0}" DecimalDigits="0" 

     

    ForceExtractValue="Always" SortExpression="Amount1" AllowFiltering="false" EmptyDataText="0"

     

    Aggregate="Sum" FooterAggregateFormatString="{0:c0}" FooterStyle-HorizontalAlign="Right" >

     

    </telerik:GridNumericColumn>

     

    Item created event

    If (TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode) Then

    Dim edititem As GridEditableItem = CType(e.Item, GridEditableItem)

    For Each GridColumn As GridColumn In edititem.OwnerTableView.Columns

    If GridColumn.IsEditable Then

    Dim editor As GridNumericColumnEditor = TryCast(edititem.EditManager.GetColumnEditor(GridColumn.UniqueName), GridNumericColumnEditor)

    If editor IsNot Nothing Then ' This is mainly to skip the other type of columns and read only numeric columns

     

    Dim cell As TableCell = CType(editor.NumericTextBox.Parent, TableCell)

     

    Dim validator As CustomValidator = New CustomValidator

    validator.ErrorMessage = "Total should be >0"

    validator.Display = ValidatorDisplay.Dynamic

     

    validator.ForeColor = Drawing.Color.DarkRed

    validator.ControlToValidate =""

    validator.ValidationGroup = "total"

    validator.ClientValidationFunction =

    "validatetotal"

    cell.Controls.Add(validator)

     

    End If

    End If

    Next

    End If


    --------------------------------------------------


    I am not even seeing the "OnServerValidate" Property.


    Please suggest a solution

    Thanks


  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Jun 2012 Link to this post

    Hello,

    Try the following code snippet to achieve your scenario.

    VB:
    Protected Sub radgrid1_ItemCreated(sender As Object, e As GridItemEventArgs)
        If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
            Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
            Dim editor As GridNumericColumnEditor = DirectCast(item.EditManager.GetColumnEditor("AMT1"), GridNumericColumnEditor)
            Dim cell As TableCell = DirectCast(editor.NumericTextBox.Parent, TableCell)
            Dim validator As New CustomValidator()
            validator.ControlToValidate = editor.NumericTextBox.ID
            validator.ErrorMessage = "Should be greater than 0"
            validator.Display = ValidatorDisplay.Dynamic
            validator.ForeColor = System.Drawing.Color.DarkBlue
            validator.ClientValidationFunction = "ClientValidationFunction"
            validator.ServerValidate += New ServerValidateEventHandler(validator_ServerValidate)
            cell.Controls.Add(validator)
        End If
    End Sub
     
    Private Sub validator_ServerValidate(source As Object, args As ServerValidateEventArgs)
        Dim val As Integer = Convert.ToInt32(args.Value)
        If val <= 0 Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If
    End Sub

    JS:
    <script type="text/javascript">
        function ClientValidationFunction(source, arguments)
         {
            if (arguments.Value <= 0)
                arguments.IsValid = false;
            else
                arguments.IsValid = true;
         }
    </script>

    Hope this helps.

    Thanks,
    Princy.
  3. NVB
    NVB avatar
    39 posts
    Member since:
    Apr 2009

    Posted 04 Jun 2012 Link to this post

    Thanks for the reply, but gets  compile errror  on

    validator.ServerValidate += NewServerValidateEventHandler(validator_ServerValidate)

    Error 1 'Public Event ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Am I missing something

    Thanks for you reply

  4. NVB
    NVB avatar
    39 posts
    Member since:
    Apr 2009

    Posted 04 Jun 2012 Link to this post

    I need to use the following

     

    AddHandler validator.ServerValidate, AddressOf validator_ServerValidate


    Thanks for your help

  5. NVB
    NVB avatar
    39 posts
    Member since:
    Apr 2009

    Posted 04 Jun 2012 Link to this post

    Thanks for the information regarding custom validator. Now  I have follow up question accessing other columns in server validation routine


    how do I get the the row index  of the  Radgrid , so I can read other columns values and do the required validation i.e totaling
    Basically I need the rowindex of the field which initiated this validation

    How do I get 'xxxx' value here
    FYI: I am doing Multitow edit, so this should  trigger for each row

     Sub validator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
            Dim editedItem As GridEditableItem
            editedItem = DirectCast(RadGridCBO.EditItems(xxxx), GridEditableItem)
         amt1= editeditem.findcontrol("amt1").text
         amt2= editeditem.findcontrol("amt2").text
         If cint(amt1) + cint(amt2) <= 0 Then
                     args.IsValid = False
         Else
                  args.IsValid = True
         End If
          
     End Sub

  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jun 2012 Link to this post

    Hello,

    I suppose you are putting all items in edit mode on pageLoad. You can access the columns in ServerValidate event as shown below.

    C#:
    void validator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            CustomValidator c = (CustomValidator)source;
            GridEditFormItem item = (GridEditFormItem)c.NamingContainer;
            int index = item.ItemIndex;
            foreach (GridEditFormItem itm in radgrid1.MasterTableView.GetItems(GridItemType.EditFormItem))
            {
                RadNumericTextBox txt1 = (RadNumericTextBox)itm["AMT1"].Controls[0];
                RadNumericTextBox txt2 = (RadNumericTextBox)itm["AMT2"].Controls[0];
                if ((txt1.Value + txt2.Value) <= 0)
                {
                  args.IsValid = false;
                }
                else
                {
                    args.IsValid = true;
                }
            }
        }

    Hope this helps.

    Thanks,
    Princy.
  7. MRa
    MRa avatar
    16 posts
    Member since:
    Dec 2012

    Posted 08 Aug 2013 Link to this post

    Just wanted to say thank you, it worked great!
Back to Top