This is a migrated thread and some comments may be shown as answers.
DatePicker's MinDate in Grid behaves differently when set from server and desgin modes
4 Answers 35 Views
This is a migrated thread and some comments may be shown as answers.
Donald
Top achievements
Rank 1
Donald asked on 30 Mar 2012, 03:12 PM
Hi,
I am using a Datepicker in a Grid and want to prevent users from selecting date beyond a MinDate.

At first i tried to set it in the ItemDataBound event:

 

 

If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then
  
Dim edititem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
  
Dim datpkr As RadDatePicker = DirectCast(edititem("DateUpdated").Controls(0), RadDatePicker)
  
datpkr.MaxDate = DateTime.Today
  
datpkr.MinDate =DateTime.Today.AddDays(-15)
End If

The issue with this is that the dates are still selectable but i get an "alert" icon in the datepicker box.. I would like the users to try to not select dates beyond mindate. (saves me writing validation code)
 
I then tried hardcoding the mindate in desgin mode as shown below:

 

<telerik:GridDateTimeColumn DataField="DateUpdated" HeaderText="DateUpdated"
  UniqueName="DateUpdated" PickerType="DatePicker" MinDate="03/01/2012" />

when i did that, i was not able to select dates before the mindate, which is what i want to do.

Question:
I really need to set the mindate from the server-side (ItemDataBound event) as the mindate would be set to another date field in the database table.
is there a way to "not select dates" les than Mindate from the server side? Or am i doing something wrong?

I did check the forum for a similar issue/solution but did not find something related.

Thanks
Donald

Am using Q1, 2012 version

4 Answers, 1 is accepted

Sort by
0
Donald
Top achievements
Rank 1
answered on 02 Apr 2012, 02:08 PM
sorry,figured a way out using it this way. (i was not using edititemtemplate)

Bascially wanted users to select dates between DateUpdated and Today. Dates beyond that range are viewable but not selectable.

<telerik:GridTemplateColumn HeaderText="Date Fixed" UniqueName="Date_Fixed"
  HeaderStyle-HorizontalAlign="Left">
  <EditItemTemplate>
     <telerik:RadDatePicker ID="dpDateFixed" runat="server" Skin="Hay" 
         MaxDate = '<%# DateTime.Now %>' MinDate='<%# Bind("DateUpdated", "{0:d}") %>' >
     </telerik:RadDatePicker>
  </EditItemTemplate>
  </telerik:GridTemplateColumn>
0
Eyup
Telerik team
answered on 02 Apr 2012, 05:03 PM
Hi Donald,

Thank you for contacting us.

I'm glad that you solved your problem. Note that your first desired behavior (to access the auto-generated DatePicker dynamically) is also achievable. You just need to change slightly your code-behind:
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)Handles RadGrid1.ItemDataBound
    If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
        Dim editForm As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim myDatePicker As RadDatePicker = TryCast(editForm("MyDateField").Controls(0), RadDatePicker)
        myDatePicker.SelectedDate = DateTime.Now.AddDays(20)
        myDatePicker.SharedCalendar.RangeMinDate = DateTime.Now.AddDays(5)
    End If
End Sub

As you probably noticed, in order to declare a MinDate to the EditForm DatePicker, you need to access the generated SharedCalendar.

Regards,
Eyup
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.
0
faishal
Top achievements
Rank 1
answered on 26 Nov 2018, 07:50 AM
please sent C# Code for the following issue
0
Eyup
Telerik team
answered on 28 Nov 2018, 05:20 PM
Hello Faishal,

Here is the C# version of the code:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem editForm = (GridEditableItem)e.Item;
        RadDatePicker myDatePicker = editForm["MyDateField"].Controls[0] as RadDatePicker;
        myDatePicker.SelectedDate = DateTime.Now.AddDays(20);
        myDatePicker.SharedCalendar.RangeMinDate = DateTime.Now.AddDays(5);
    }
}

You can also check the following section:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

In addition, here is the Telerik converter which you can check in similar scenarios:
http://converter.telerik.com/

I hope this will prove helpful.

Regards,
Eyup
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.
Tags
Grid
Asked by
Donald
Top achievements
Rank 1
Answers by
Donald
Top achievements
Rank 1
Eyup
Telerik team
faishal
Top achievements
Rank 1
Share this question
or