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

Compare two date picker values in RadGrid....

1 Answer 107 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kallappa
Top achievements
Rank 1
Kallappa asked on 09 Jul 2012, 06:47 AM
Hi,
I have a rad grid that contains two columns for StartDate and EndDate with RadDatePickers.
i want to compare that the date selected in "EndDate" should not be less than the date selected in "StartDate"
or start date should be less than end date.
How can i compare the both the dates in grid.My column type is GridTemplateCoulmn and both the Start and End date pickers are in ItemTemplate.
below is my code.

                 <telerik:GridTemplateColumn HeaderText="Start" UniqueName="fispersrt" DataField="fispersrt"
                                Visible="true" FilterControlWidth="78%" AllowFiltering="true">
                                <ItemTemplate>
                                    <telerik:RadDatePicker ID="rdDtStrtpkr" runat="server" SelectedDate='<%#Eval("fispersrt")%>'>
                                    </telerik:RadDatePicker>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <telerik:RadDatePicker ID="rdDtFootrStrtpkr" runat="server">
                                    </telerik:RadDatePicker>
                                </FooterTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="End" UniqueName="fisperend" DataField="fisperend" FilterControlWidth="78%" AllowFiltering="true">
                                <ItemTemplate>
                                    <telerik:RadDatePicker ID="rdDtEndpkr" runat="server" SelectedDate='<%#Eval("fisperend")%>'>
                                    </telerik:RadDatePicker>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <telerik:RadDatePicker ID="rdDtFootrEndpkr" runat="server">
                                    </telerik:RadDatePicker>
                                </FooterTemplate>
                            </telerik:GridTemplateColumn>

Thanks in Advance.

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 09 Jul 2012, 07:26 AM
Hello,

You can use a CompareValidator for comparing the dates. Here is the sample code.
aspx:
<telerik:GridTemplateColumn>
  <ItemTemplate>
     <telerik:RadDatePicker ID="rdDtEndpkr" runat="server" ></telerik:RadDatePicker>
      <asp:CompareValidator ID="CompareValidator1" runat="server"></asp:CompareValidator>
  </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
  {
      GridDataItem item = (GridDataItem)e.Item;
       RadDatePicker picker1 = (RadDatePicker)item.FindControl("rdDtStrtpkr");
       DateTime startDate = Convert.ToDateTime(picker1.SelectedDate);
       RadDatePicker picker2 = (RadDatePicker)item.FindControl("rdDtEndpkr");
       CompareValidator compValidate = (CompareValidator)item.FindControl("CompareValidator1");
       compValidate.ControlToValidate = picker1.ID;
        compValidate.ControlToCompare = picker2.ID;
        compValidate.Type = ValidationDataType.Date;
        compValidate.Operator = ValidationCompareOperator.LessThanEqual;
        compValidate.ErrorMessage = "EndDate should not be less than StartDate";
  }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Kallappa
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or