How to use RadDatePicker inside RadGrid?

612 Views Asked by At

Wasn't able to find anything about this situation:

I have two RadDatePicker inside RadGrid for start and end date change

<telerik:RadDatePicker ID="rdpStartDate" Skin="Library" EnableEmbeddedSkins="false" CommandName="StartDateChange" runat="server" />

By itself, they work fine, but now I have a situation when I need to call method when their value has been changed (CommandName was added for this)

I know how to do this outside RadGrid, basically:

Protected Sub rdpStartDateChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs) Handles rdpStartDate.SelectedDateChanged
  ...
  ...
End Sub

But I wasn't able to do this inside RadGrid, because nothings seems to trigger it.

I tried to catch my command with this (works for buttons at least):

Protected Sub rgLibraryItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rgLibrary.ItemCommand

But, no, it doesn't see CommandName="StartDateChange"

What I need to do to be able to catch those Date change events if RadDatePicker is placed inside RadGrid?

2

There are 2 best solutions below

0
On

You need to identify the control inside the grid which triggers the action. I'm not sure which is the way for Rad to do that but it should be similar this:

Private Sub DataGridView1_ButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) _
                                      Handles DataGridView1.CellButtonClick
    'TODO - Button Clicked - Execute Code Here

End Sub

So you need to find the events for the DataGridViewRow for Rad and substitute them with the ones cell clicking events. This example should get you started.

0
On

Subscribing to an event on a control inside a RadGrid does not work the same way, because there could be multiple copies of the control or even none at all, depending on how many records are in the Data Source. Therefore, in order to subscribe to events on these controls, you have to do it manually after the data is bound, either in the ItemCreated event or ItemDataBound event.

Protected Sub rgLibraryItemCreated(ByVal sender as Object, ByVal e As GridItemEventArgs) Handles rgLibrary.ItemCreated
    If TypeOf e.Item Is GridDataItem Then
        Dim item As GridDataItem = e.Item
        Dim rdpStartDate As RadDatePicker = item.FindControl("rdpStartDate")
        AddHandler rdpStartDate.SelectedDateChanged, AddressOf rdpStartDateChanged
    End If
End Sub