Addig a GridViewTemplate to a RadGridView stops the DragAndDropService?

1 Answer 63 Views
GridView
Tibor
Top achievements
Rank 1
Iron
Iron
Tibor asked on 26 Sep 2023, 08:32 AM | edited on 26 Sep 2023, 08:33 AM

The Drag & Drop service is working perfectly on my RadGridView until I add a GridViewTemplate to it with the intent to create a hierarchy view, after which the drag events aren't even fired anymore. Any ideas, please?

 

This is how I initialize the Drag & Drop behavior on my main DataGrid:

 

private void InitGrid()
        {             
            bt_addAttachment.Visible = false;
            BaseGridBehavior gridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomRowGridBehavior());

            //handle drag and drop events for the grid through the DragDrop service
            RadDragDropService svc = this.radGridView1.GridViewElement.GetService<RadDragDropService>();
            svc.PreviewDragStart += svc_PreviewDragStart;
            svc.PreviewDragDrop += svc_PreviewDragDrop;
            svc.PreviewDragOver += svc_PreviewDragOver;        
        }

        //required to initiate drag and drop when grid is in bound mode
        private void svc_PreviewDragStart(object sender, PreviewDragStartEventArgs e)
        {
            e.CanStart = true;
        }

        private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
        {
            if (e.DragInstance is GridDataRowElement)
            {
                e.CanDrop = e.HitTarget is SchedulerCellElement;
            }
        }

        private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
        {
            SchedulerCellElement schedulerCell = e.HitTarget as SchedulerCellElement;
            if (schedulerCell == null)
            {
                //DayViewAllDayHeader allDay = (this.radScheduler1.SchedulerElement.ViewElement as SchedulerDayViewElement).AllDayHeaderElement;
                //schedulerCell = SchedulerUIHelper.GetCellAtPoint(e.DropLocation, allDay.Children);
            }
            if (schedulerCell == null)
            {
                return;
            }
            GridDataRowElement draggedRow = e.DragInstance as GridDataRowElement;
            if (draggedRow == null)
            {
                return;
            }

            DataRowView dataRowView = draggedRow.Data.DataBoundItem as DataRowView;
            if (dataRowView != null)
            {
                if (draggedRow.GridControl.DataSource != null && typeof(BindingSource).IsAssignableFrom(draggedRow.GridControl.DataSource.GetType()))
                {
                    Appointment appointment = new Appointment();
                    appointment.Start = (DateTime)draggedRow.RowInfo.Cells["Start"].Value;
                    appointment.End = (DateTime)draggedRow.RowInfo.Cells["End"].Value;
                    //adjust start/end according to target cell
                    appointment.End = schedulerCell.Date.AddMinutes(appointment.Duration.TotalMinutes);
                    appointment.Start = schedulerCell.Date;
                    appointment.Summary = string.Empty + draggedRow.RowInfo.Cells["Summary"].Value;
                    appointment.Description = string.Empty + draggedRow.RowInfo.Cells["Description"].Value;
                    appointment.Location = string.Empty + draggedRow.RowInfo.Cells["Location"].Value;
                    appointment.StatusId = (int)draggedRow.RowInfo.Cells["StatusId"].Value;
                    appointment.BackgroundId = (int)draggedRow.RowInfo.Cells["BackgroundId"].Value;
                    //this.radScheduler1.Appointments.Add(appointment);

                    dataRowView.Row.Table.Rows.Remove(dataRowView.Row);
                }
                else
                {
                    throw new ApplicationException("Unhandled Scenario");
                }
            }
        }

 

Which is working perfectly until I add this piece of code to it:

 

///attachments sub-row initialization
GridViewTemplate attachmentsTemplate = new GridViewTemplate();
attachmentsTemplate.AutoGenerateColumns = false;            
attachmentsTemplate.HierarchyDataProvider = new GridViewEventDataProvider(attachmentsTemplate);

GridViewTextBoxColumn columnMainJobID = new GridViewTextBoxColumn();
columnMainJobID.TextAlignment = ContentAlignment.MiddleCenter;
columnMainJobID.FieldName = "MainJobID";
columnMainJobID.IsVisible = false;
attachmentsTemplate.Columns.Add(columnMainJobID);

radGridView1.Templates.Add(attachmentsTemplate);

 

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 27 Sep 2023, 11:46 AM

Hello, Tibor,

RadGridView handles the whole drag-and-drop operation by its RadGridViewDragDropService. If I understand you correctly, you would like to create a hierarchy grid. You can achieve drag-drop functionality with a hierarchy grid as well. I reviewed the provided code snippets and noticed that you added a GridViewTemplate and added it to RadGridView.Templates collection.

However, according to the provided code snippet it is not clear whether you define a GridViewRelation. Could you please clarify if the hierarchy grid set up is correct on your side? It is important to add a GridViewRelation which makes a relation between the grid's templates. Please refer to the following article where you can see how the relation is made when adding a child template to build a hierarchy grid: Object Relational Hierarchy Mode - WinForms GridView Control - Telerik UI for WinForms 

For your reference, I have prepared a sample project with a hierarchical grid and drag-drop service. If you are still experiencing any further difficulties, I would kindly ask you to modify the project in a way to replicate the undesired behavior. Thus, we would be able to make an adequate analysis of the problem and assist you further. Thank you in advance.

I hope this information helps. If you have any additional questions, please let me know. 

Regards,
Nadya
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Tibor
Top achievements
Rank 1
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or