We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GetVisualContainer() is not found on sfDataGrid

I have downloaded the following sample and then built it.
http://www.syncfusion.com/kb/4116/how-to-drag-and-drop-the-row-data-from-one-sfdatagrid-to-another

Got the following error:
error CS1061: 'Syncfusion.UI.Xaml.Grid.SfDataGrid' does not contain a definition for 'GetVisualContainer' and no extension method 'GetVisualContainer' accepting a first argument of type 'Syncfusion.UI.Xaml.Grid.SfDataGrid' could be found (are you missing a using directive or an assembly reference?)

Looked out for solution, following is recommended-
using Syncfusion.UI.Xaml.Grid.Helpers;
But that did not work.

===================================================================================================================================================================
Here is the complete file----  

using System.Collections.ObjectModel;
using Syncfusion.Data;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.ScrollAxis;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using Syncfusion.UI.Xaml.Grid.Helpers;

namespace SfDataGrid_RowDragAndDrop
{
    public class GridSelectionControllerExt:GridSelectionController
    {

        VisualContainer visualContainer;
        public GridSelectionControllerExt(SfDataGrid datagrid)
            : base(datagrid)
        {
            //Loaded event of SfDataGrid is wired here
            datagrid.Loaded += datagrid_Loaded;
            //Unloaded event of SfDataGrid is wired here
            datagrid.Unloaded += datagrid_Unloaded;
        }
             
        void datagrid_Loaded(object sender, RoutedEventArgs e)
        {
            //You can get the VisualContainer ,with the help of GetVisualContainer() helper method 

            visualContainer = DataGrid.GetVisualContainer();

            //Fired when the row data is dropped on the specified position
            visualContainer.PreviewDrop += visualContainer_PreviewDrop;
            //Fired when the mouse operation is performed   
            visualContainer.PreviewMouseMove += visualContainer_PreviewMouseMove;
        }
                
        
        //Initially IsPointerPressed will be false 
        public bool IsPointerPressed = false;
        //Intially the PressedRowColumnIndex will be empty
        RowColumnIndex PressedRowColumnIndex = RowColumnIndex.Empty;
        protected override void ProcessPointerPressed(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
        {
            //IsPointerPressed will be true if the pointer is pressed
            IsPointerPressed = true;
            //The currently pressed rowColumnIndex is assigned to PressedRowColumnIndex
            PressedRowColumnIndex = rowColumnIndex;
            base.ProcessPointerPressed(args, rowColumnIndex);
        }

        void visualContainer_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            var rowColumnIndex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
            if (!rowColumnIndex.IsEmpty && IsPointerPressed)
            {
                // Get the RecordIndex using RowIndex
                var index = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);                
                if (index != -1)
                {
                    // Get the RecordIndex using RowIndex
                    var recordIndex = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
                    // Get the Record Data as RecordEntry using RecordIndex 
                    var recordEntry = DataGrid.View.Records[recordIndex];
                    var data = new DataObject();
                    //Stores the specified DataGrid to this data, along with specified format  "DataGrid"
                    data.SetData("DataGrid", DataGrid);
                    //Stores the specified recordEntry to this data, along with specified format "RecordEntry"
                    data.SetData("RecordEntry", recordEntry);
                    //Initiate the drag and drop operation for the specified source and data with  permitted drag and drop effects 
                    DragDrop.DoDragDrop(DataGrid, data, DragDropEffects.Copy | DragDropEffects.Move);
                }
            }
        }

        private void visualContainer_PreviewDrop(object sender, DragEventArgs e)
        {
            //Checks whether  the datagrid/recordentry is available on the format
            if (!e.Data.GetDataPresent("DataGrid") || !e.Data.GetDataPresent("RecordEntry"))
                return;
            //Get the current data grid whose row to be dragged
            var dragginggrid = e.Data.GetData("DataGrid") as SfDataGrid;
            //Get the record entry from the row of dragging grid
            var recordent = e.Data.GetData("RecordEntry") as RecordEntry;
            //Check whether the dragginggrid and record is not equal to null
            if (dragginggrid == null || recordent == null)
                return;
            //After dropping the row the IsPointerPressed state should be false
            (dragginggrid.SelectionController as GridSelectionControllerExt).IsPointerPressed = false;
            // Get the  current  RowColumnIndex based on the VisualContainer posotion
            var rowColumnIndex = visualContainer.PointToCellRowColumnIndex(e.GetPosition(visualContainer));
            var draggingGrid_Source = dragginggrid.ItemsSource as ObservableCollection<Student>;
            var droppingGrid_Source = DataGrid.ItemsSource as ObservableCollection<Student>;
            if (!rowColumnIndex.IsEmpty)
            {
                // Get the RecordIndex using RowIndex
                var index = DataGrid.ResolveToRecordIndex(rowColumnIndex.RowIndex);
                //if the dragged row data is dropped inside the DataRow , then it will be processed here.
                if (index != -1)
                {
                    // Remove the Selected Row Data from Dragging SfDataGrid 
                    draggingGrid_Source.Remove(recordent.Data as Student);
                    // Insert the Selected row data to specified index position of dropping SfDataGri
                    droppingGrid_Source.Insert(index, recordent.Data as Student);
                    // Selected Item is reset with new dropped row data
                    DataGrid.SelectedItem = droppingGrid_Source[index];
                    this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
                }
                //if the dragged row data is dropped on the HeaderRow ,then it will be processed here.
                else
                {                    
                    // Insert the Selected row data to specified index position of dropping SfDataGrid
                    droppingGrid_Source.Insert(index + 1, recordent.Data as Student);
                    // Remove the Selected Row Data from Dragging SfDataGrid 
                    draggingGrid_Source.Remove(recordent.Data as Student);
                    // Selected Item is reset with new dropped row data
                    DataGrid.SelectedItem = recordent.Data;
                    this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
                }
            }
                //if the dragged row data is dropped in empty space of SfDataGrid , it will be processed here
            else
            {
                //IsPointerPressed will be false if the pointer is released
                IsPointerPressed = false;
                //Get the record count of the dropping grid ,Since the row and column index is empty
                var recordcount = this.DataGrid.View.Records.Count;                
                // Remove the Selected Row Data from Dragging SfDataGrid 
                draggingGrid_Source.Remove(recordent.Data as Student);
                // Insert the Selected row data to specified index position of dropping SfDataGri
                droppingGrid_Source.Insert(recordcount, recordent.Data as Student);
                // Selected Item is reset with new dropped row data
                DataGrid.SelectedItem = droppingGrid_Source[recordcount];
                //Move the selection to currently dropped row
                rowColumnIndex.RowIndex = recordcount + 1;
                this.MoveCurrentCell(new RowColumnIndex(rowColumnIndex.RowIndex, 1));
            }
        }

        protected override void ProcessPointerReleased(MouseButtonEventArgs args, RowColumnIndex rowColumnIndex)
        {
            //IsPointerPressed will be false if the pointer is released
            IsPointerPressed = false;
            base.ProcessPointerReleased(args, rowColumnIndex);
        }

        void datagrid_Unloaded(object sender, RoutedEventArgs e)
        {
            //Unwire events
            DataGrid.Loaded -= datagrid_Loaded;
            visualContainer.PreviewDrop -= visualContainer_PreviewDrop;
            visualContainer.PreviewMouseMove -= visualContainer_PreviewMouseMove;
            DataGrid.Unloaded -= datagrid_Unloaded;
        }
    }
}


3 Replies

AN Ashok N Syncfusion Team May 22, 2015 08:40 AM UTC

Hi Soumyajit ,

Thank you for contacting Syncfusion support.

We have analyzed your query. We cannot access GetVisualContainer() helper method in product version 12.3 since its available only from version 12.4 . So, we request you to upgrade your product version, otherwise you can get VisualContainer by using Reflection because in 12.3 we have VisualContainer is internal. Please refer the below code example for getting VisualContainer using reflection:

VisualContainer visualContainer = this.DataGrid.GetType().GetProperty("VisualContainer",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this.DataGrid) as VisualContainer;

We have modified a sample byusing Reflection and attached the sample in the following location, please refer it :

Sample location: http://www.syncfusion.com/downloads/support/forum/119201/RowDrag-and-DropInSfDataGrid_WPF-1458758221.zip

Please let us know if you require further assistance on this .

Thanks,

Ashok



SR Soumyajit Roy May 22, 2015 09:14 AM UTC

Thank You, Ashok.
I have already tried with the latest version 13.1 and it worked. I will definitely upgrade the library to the latest version.


SC Saranya CJ Syncfusion Team May 25, 2015 06:47 AM UTC

Hi Soumyajit,

Thank you for your update. Please let us know if you require any other assistance on this.

Regards,
Saranya

Loader.
Up arrow icon