Extract the new RadGrid values in the UpdateCommand event

4 posts, 0 answers
  1. Carlos
    Carlos avatar
    4 posts
    Member since:
    Oct 2018

    Posted 12 Nov 2018 Link to this post

    Hi, I need to extract the new values from the GridDateTimeColumn to validate that one date is not greater than or less than another as needed. Do this but only extract the previous values in the edited items.

    <MasterTableView DataSourceID="sdsControlGranel" AutoGenerateColumns="False" CommandItemDisplay="Top" EditMode="Batch">
                <CommandItemSettings SaveChangesText="Guardar cambios" CancelChangesText="Cancelar cambios" ShowExportToExcelButton="True" ShowAddNewRecordButton="false"></CommandItemSettings>
                <BatchEditingSettings EditType="Row" />
                <Columns>
                    <telerik:GridDateTimeColumn DataField="FcAsignacion" HeaderText="Fecha Asignación" SortExpression="FcAsignacion" UniqueName="FcAsignacion" DataType="System.DateTime" PickerType="DateTimePicker" FilterControlAltText="Filter FcAsignacion column" CurrentFilterFunction="Contains">
    </telerik:GridDateTimeColumn>
                    <telerik:GridDateTimeColumn DataField="FcInicioOperacion" HeaderText="Fecha Inicio" SortExpression="FcInicioOperacion" UniqueName="FcInicioOperacion" DataType="System.DateTime" PickerType="DateTimePicker" FilterControlAltText="Filter FcInicioOperacion column" CurrentFilterFunction="Contains">
    </telerik:GridDateTimeColumn>
                    <telerik:GridDateTimeColumn DataField="FcFinalOperacion" HeaderText="Fecha Final" SortExpression="FcFinalOperacion" UniqueName="FcFinalOperacion" DataType="System.DateTime" PickerType="DateTimePicker" FilterControlAltText="Filter FcFinalOperacion column" CurrentFilterFunction="Contains">
    </telerik:GridDateTimeColumn>
                </Columns>
            </MasterTableView>
     

     

    protected void rgControlGranel_UpdateCommand(object sender, GridCommandEventArgs e)
            {
                GridEditableItem editItem = e.Item as GridEditableItem;
                Hashtable newValues = new Hashtable();
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editItem);
                DateTime fcAsignacion = Convert.ToDateTime(newValues["FcAsignacion"].ToString());
                DateTime fcInicio = Convert.ToDateTime(newValues["FcInicioOperacion"].ToString());
     
                if (DateTime.Compare(fcAsignacion, fcInicio) > 0)
                {
                    e.Canceled = true;
                    this.TextoMensaje("La fecha de asignación no puede ser mayor a la de inicio", 3);
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "MostrarMensaje();", true);
                }
            }

     

     

     

     

  2. Attila Antal
    Admin
    Attila Antal avatar
    204 posts

    Posted 15 Nov 2018 Link to this post

    Hi Carlos,

    The Batch Editing Server-side API documentation articles describes the steps to extract new and old values from RadGrid with BatchEditing. Also, when working with this EditMode, using the Batch Editing Client-side API would be a better fit for validations. 

    For example, you could wire up some of the client-side events for BathEditing (OnBatchEditOpening, OnBatchEditOpenedOnBatchEditClosing, OnBatchEditClosed), access the controls in the currently edited row/cell, do the validation on the control and only then let the grid do a post back to the server where it will send the old and new values.

    I hope this will prove helpful.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. Carlos
    Carlos avatar
    4 posts
    Member since:
    Oct 2018

    Posted 19 Nov 2018 in reply to Attila Antal Link to this post

    Hi Attila, could you show me an example of how to validate dates with the Batch Editing Client-side API? I just do not understand how it works.

    Thank you!!
  4. Attila Antal
    Admin
    Attila Antal avatar
    204 posts

    Posted 22 Nov 2018 Link to this post

    Hi Carlos,

    I've almost forgot to mention that you will need to add a validation key to the web.config within the appSettings tag. This is described in the first note in the jQuery Troubleshooting article. 
    <appSettings>
      <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
    </appSettings>

    Below, I will try to point out some of the important parts from the RadGrid Batch Editing Validation article.

    You can create a template column in the grid similar to the code below and add a Custom Validator that calls the validateInput JavaScript method.
    <telerik:GridTemplateColumn UniqueName="OrderDate" DataField="OrderDate" HeaderText="Order Date" DataType="System.DateTime">
        <ItemTemplate>
            <telerik:RadLabel ID="RadLabel1" runat="server" Text='<%# Eval("OrderDate").ToString() %>' Width="100px"></telerik:RadLabel>
        </ItemTemplate>
        <EditItemTemplate>
            <telerik:RadDatePicker ID="RadDatePicker1" runat="server"></telerik:RadDatePicker>
     
            <asp:CustomValidator runat="server" ID="cvAcdntDt"
                ControlToValidate="RadDatePicker1"
                ClientValidationFunction="validateInput"
                ForeColor="Red"
                ErrorMessage="*Order Date is required"
                Display="Dynamic">
            </asp:CustomValidator>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    JavaScript method where you can use the API client-side API of Grid Batch editing as well as the one for the DatePicker, access the values, validate them and if they do not meet the requirements, set the validator to Invalid.
    function validateInput(source, arguments) {
        //use the control's API, validate values and if condition is not met, set the IsValid property to false.
        if (0 != 1) {
            arguments.IsValid = false;
        }
    }

    Luckily I had a sample project demonstrating this behavior and I attached it to my response. 

    I hope this will help understanding validation with BatchEditing.

    Kind regards,
    Attila Antal
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top