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

Custom Validation for GridDateTimeColumn

4 Answers 136 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Elliott
Top achievements
Rank 2
Elliott asked on 15 Jul 2011, 03:01 PM
I need to apply custom validation on a GridDateTimeColumn

is there a way to add a custom validator on the Edit Command?

or
if I template the column, what is the equivalent (Label and RADDatePicker?) in ItemTemplate controls and EditItemTemplate controls

4 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 20 Jul 2011, 09:53 AM
Hi Marianne,

Yes, there is a way to add custom validation, you could find more information about it at the last section of our online documentation:
http://www.telerik.com/help/aspnet-ajax/grid-custom-editors.html

The other approach you have mentioned is applicable too, more information you could find in our Getting Started section, sub-section Add Controls to Column Templates of RadGrid online documentation which resides on the following address:
http://www.telerik.com/help/aspnet-ajax/grid-add-controls-to-templates.html

Kind regards,
Andrey
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Elliott
Top achievements
Rank 2
answered on 20 Jul 2011, 02:44 PM
thanks Andrey

I ended up adding a custom validator but couldn't get it to fire - there is a javascript routine that needed to run before any custom validator
    var j = 0;
    function ship_date_selected(sender, eventArgs) {
        var date = eventArgs._newValue;
        // prevent loop
//        var chk = eventArgs._oldValue;
        if (j > 0) {
            return;
        }
        var oldDate = eventArgs.get_oldDate();
        var thisItem = document.getElementById('txtItemID');
        var thisStore = document.getElementById('txtStoreHidden');
          
        // date already for Item
        var theGrid = $find("<%=rgEditOrder.ClientID %>").get_masterTableView();
        for (var i = 0; i < theGrid.get_dataItems().length; i++) {
            var row = theGrid.get_dataItems()[i];
            if (!row.get_isInEditMode()) {
                var ItemID = theGrid.getCellByColumnUniqueName(row, "ItemID").innerHTML;
                var storeNumber = theGrid.getCellByColumnUniqueName(row, "StoreNumber").innerHTML;
//              alert(ItemID);
                if (parseInt(ItemID) == parseInt(thisItem.value)) {
                    if (parseInt(storeNumber) == parseInt(thisStore.value)) {
                        var ShipDate = theGrid.getCellByColumnUniqueName(row, "ShipDate").innerHTML;
                        if (Date.parse(date) == Date.parse(ShipDate)) {
                            alert('Date already used ' + ShipDate);
                            sender.set_selectedDate(oldDate);
                            return false;
                        }
                    }
                }
            }
        }
          
        var strSD = format_dates(date);
        if (!delivery_ok(strSD)) {
            j++;
            sender.set_selectedDate(oldDate);
            alert('Invalid delivery date');
            return false;
            }
    }
    // validate that date supplied falls in one of three ship windows        
    function delivery_ok(strSD) {
        var endDate1 = document.getElementById('rtbEnd1');
        var endDate2 = document.getElementById('rtbEnd2');
        var endDate3 = document.getElementById('rtbEnd3');
        var startDate1 = document.getElementById('rtbStart1');
        var startDate2 = document.getElementById('rtbStart2');
        var startDate3 = document.getElementById('rtbStart3');
  
        // add century(20) to ship dates-mm/dd/yy->mm/dd/yyyy            
        var vDate1 = format_dates(startDate1.value);
        var vDate2 = format_dates(startDate2.value);
        var vDate3 = format_dates(startDate3.value);
        var vEnd1 = format_dates(endDate1.value);
        var vEnd2 = format_dates(endDate2.value);
        var vEnd3 = format_dates(endDate3.value);
  
        if (vDate1 != '') {
            if (Date.parse(strSD) >= Date.parse(vDate1) && Date.parse(strSD) <= Date.parse(vEnd1)) {
                return true;
            }
        }
        if (vDate2 != '') {
            if (Date.parse(strSD) >= Date.parse(vDate2) && Date.parse(strSD) <= Date.parse(vEnd2)) {
                return true;
            }
        }
        if (vDate3 != '') {
            if (Date.parse(strSD) >= Date.parse(vDate3) && Date.parse(strSD) <= Date.parse(vEnd3)) {
                return true;
            }
        }
        return false;
    }
the grid
        <telerik:RadGrid ID="rgEditOrder" OnNeedDataSource="rgEditOrder_NeedDataSource" 
            OnItemCommand="rgEditOrder_ItemCommand" OnItemCreated="rgEditOrder_ItemCreated" 
            ShowFooter="True" runat="server" Skin="Sunset">
        <GroupingSettings CaseSensitive="false" />
        <MasterTableView DataKeyNames="OrderSeq" AutoGenerateColumns="false" AllowSorting="true" AllowPaging="true" AllowFilteringByColumn="False" EditMode="InPlace" >
        <Columns>
            <telerik:GridBoundColumn UniqueName="StoreNumber" DataField="StoreNumber" HeaderText="Store #" DataFormatString="{0:#####}" DataType="System.Int32" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="BoothNumber" DataField="BoothNumber" HeaderText="Booth #" DataFormatString="{0:#####}" DataType="System.Int32" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="ItemID" DataField="ItemID" HeaderText="Item" DataType="System.Double" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="ItemDescription" DataField="ItemDescription" HeaderText="Description" ReadOnly="True">
                <HeaderStyle Width="100px" />
            </telerik:GridBoundColumn>
            <telerik:GridDateTimeColumn UniqueName="ShipDate" DataField="ShipDate" PickerType="DatePicker" HeaderText="Ship Date" DataFormatString="{0:d}" DataType="System.DateTime" >
                <HeaderStyle Width="60px" />           
            </telerik:GridDateTimeColumn>
            <telerik:GridTemplateColumn UniqueName="Cases" HeaderText="" >
                <EditItemTemplate>
                    <telerik:RadNumericTextBox ID="rntbQty" Text='<%# Eval("Qty") %>' MinValue='<%# Bind("Qty") %>' OnTextChanged="rntbQty_TextChanged" AutoPostBack="True" Width="30px" runat="server">
                        <NumberFormat DecimalDigits="0" />
                    </telerik:RadNumericTextBox>
                </EditItemTemplate>
                <HeaderStyle Width="30px" />            
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn UniqueName="Qty" DataField="Qty" HeaderText="Cases" DataFormatString="{0:#####}" DataType="System.Int32" Aggregate="Sum" ReadOnly="True" >
                <HeaderStyle Width="30px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="NetCost" DataField="NetCost" HeaderText="Net Cost" DataFormatString="{0:C2}" DataType="System.Double" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="50px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="ItemTotal" DataField="ItemTotal" HeaderText="Item Total" DataFormatString="{0:C2}" DataType="System.Double" Aggregate="Sum" ReadOnly="True">
                <HeaderStyle Width="50px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="PageNumber" DataField="PageNumber" HeaderText="Page" DataFormatString="{0:#####}" DataType="System.Int32" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="VendorNumber" DataField="VendorNumber" HeaderText="Vendor" DataFormatString="{0:#####}" DataType="System.Int32" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="40px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="DepartmentNumber" DataField="DepartmentNumber" HeaderText="Depart" DataFormatString="{0:##}" DataType="System.Int32" Aggregate="None" ReadOnly="True">
                <HeaderStyle Width="30px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn UniqueName="OrderSeq" DataField="OrderSeq" DataType="System.Int64" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridDateTimeColumn UniqueName="StartDate1" DataField="StartDate1" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>   
            <telerik:GridDateTimeColumn UniqueName="EndDate1" DataField="EndDate1" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>
            <telerik:GridDateTimeColumn UniqueName="StartDate2" DataField="StartDate2" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>   
            <telerik:GridDateTimeColumn UniqueName="EndDate2" DataField="EndDate2" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>
            <telerik:GridDateTimeColumn UniqueName="StartDate3" DataField="StartDate3" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>   
            <telerik:GridDateTimeColumn UniqueName="EndDate3" DataField="EndDate3" PickerType="DatePicker" DataType="System.DateTime" Visible="false">
            </telerik:GridDateTimeColumn>
        </Columns>
        </MasterTableView>
        <ClientSettings>
            <ClientEvents OnRowDblClick="RowDblClick" />
            <ClientEvents OnRowClick="RowClick" />
<%--        <ClientEvents OnGridCreated="GetGridObject" />
            <ClientEvents OnDataSourceResolved="CalcTotals" />--%>
        </ClientSettings>
        </telerik:RadGrid>

I fire the validation on the update, which is fine - it's on the server anyway

protected void rgEditOrder_ItemCreated(object source, GridItemEventArgs e)
{
    GridEditableItem geItem;
    RadDatePicker rdpShipDate;
    GridDateTimeColumnEditor ceShipDate;
    TableCell tcShipDate;
    CustomValidator cvShipDate;
    if (e.Item.IsInEditMode)
    { }
    else
        return;
    if (e.Item is GridEditableItem)
    { }
    else
        return;
    geItem = (GridEditableItem)e.Item;
    rdpShipDate = (geItem["ShipDate"].Controls[0] as RadDatePicker);
    rdpShipDate.ClientEvents.OnDateSelected = "ship_date_selected";
    rdpShipDate.AutoPostBack = true;     
    rdpShipDate.SelectedDateChanged += rdpShipDate_SelectedDateChanged;
    txtShipDate.Value = rdpShipDate.ClientID;
    ceShipDate = geItem.EditManager.GetColumnEditor("ShipDate") as GridDateTimeColumnEditor;
    tcShipDate = (TableCell)ceShipDate.PickerControl.Parent;
    cvShipDate = new CustomValidator();
    cvShipDate.ID = "cvShipDate";
    cvShipDate.ControlToValidate = rdpShipDate.ID;
    cvShipDate.ServerValidate += ShipDate_OnServerValidate;
    cvShipDate.Text = "*";
    cvShipDate.ErrorMessage = "Item already ordered on this ship date";
    tcShipDate.Controls.Add(cvShipDate);
}

protected void rdpShipDate_SelectedDateChanged(object sender, EventArgs e)
{
    RadDatePicker rdpShipDate;
    GridEditableItem geItem;
    DateTime OldShipDate,NewShipDate;
    double fStore,dItemID;
    int iBooth,iPage,iQty;
    string strOldDel,strNewDel;
    bool bUpdate=false;
    WsOrderSystem wsOrder;
    Page.Validate();
    if (Page.IsValid) {}
    else { return; }
    rdpShipDate = (sender as RadDatePicker);
    geItem = rdpShipDate.NamingContainer as GridEditableItem;
    NewShipDate = Convert.ToDateTime(rdpShipDate.SelectedDate);
    OldShipDate = Convert.ToDateTime((e as Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs).OldDate);
    fStore = Convert.ToDouble(ExtractValue(geItem,"StoreNumber"));
    iBooth = Convert.ToInt32(ExtractValue(geItem,"BoothNumber"));
    dItemID = Convert.ToDouble(ExtractValue(geItem,"ItemID"));
    iPage = Convert.ToInt32(ExtractValue(geItem,"PageNumber"));
    iQty = Convert.ToInt32(ExtractValue(geItem, "Qty"));
    strOldDel = OldShipDate.ToShortDateString();
    strNewDel = NewShipDate.ToShortDateString();
    wsOrder = new WsOrderSystem();        
    bUpdate = wsOrder.UpdateOrder(fStore,iBooth,iPage,dItemID,iQty,strOldDel,strNewDel);
    rgEditOrder.MasterTableView.IsItemInserted = false;
}
protected void ShipDate_OnServerValidate(object source, ServerValidateEventArgs args)
{
    CustomValidator cvShipDate;
    RadDatePicker rdpShipDate;
    GridEditableItem geItem;
    TableCell tcShipDate;
    double fStore, dItemID;
    int iBooth, iPage, iQty;
    string strDel;
    WsOrderSystem wsOrder;
    DataSet dsOrder;
    lblCompleteOrder.Visible = false;
    args.IsValid = false;
    cvShipDate = (CustomValidator)source;
    tcShipDate = (TableCell) cvShipDate.Parent;
    geItem = (GridEditableItem) tcShipDate.Parent;
    strDel = args.Value;
    fStore = Convert.ToDouble(ExtractValue(geItem, "StoreNumber"));
    iBooth = Convert.ToInt32(ExtractValue(geItem, "BoothNumber"));
    dItemID = Convert.ToDouble(ExtractValue(geItem, "ItemID"));
    iPage = Convert.ToInt32(ExtractValue(geItem, "PageNumber"));
    wsOrder = new WsOrderSystem();
    dsOrder = new DataSet();
    dsOrder = wsOrder.GetOneOrderItem(fStore,iBooth,iPage,dItemID,strDel);
    if (dsOrder.Tables.Count > 0)
    {
        if (dsOrder.Tables[0].Rows.Count < 1)
            args.IsValid = true;
    }
}
0
Muhammad
Top achievements
Rank 1
answered on 09 Aug 2012, 07:46 AM
i am making a page where i need some validation that StartDate should be less than the EndDate and i m using telerik:GridDateTimeColumn. Please tell me hoe can i compare these two values from telerik:GridDateTimeColumn.
its ergent please.....:-/
0
Andrey
Telerik team
answered on 13 Aug 2012, 04:09 PM
Hello,

You could check this online demo application for a sample code of the approach you could use.

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Elliott
Top achievements
Rank 2
Answers by
Andrey
Telerik team
Elliott
Top achievements
Rank 2
Muhammad
Top achievements
Rank 1
Share this question
or