DragDrop between 3 RadGridView

2 posts, 0 answers
  1. neha
    neha avatar
    5 posts
    Member since:
    May 2010

    Posted 28 Jul 2010 Link to this post

    Hello,

    I have 3 RadGridViews out of which one is the source and two are destinations. The two destination radgridviews are on the RadTab Control. Drag and Drop functionality works fine between Source and one of the destination except that the events are not hit for the dropping position of the Row (Insert after/before row not seen).

    When I try to drag the source onto the second radgridview destination, the drop functionality doesnt seem to work (Drag is showing me the object dragged, but drop isnt showing any text of adding.... I have noticed the e.Option.Status is always DragCancel when i try to drop the row in the 2 grid).

    I did make a copy of the 1st destination (working) and tried to include it as the 2nd destination, but still no luck in getting it to work. Also I did make copy of the events which I could consume conditionally thinking that it is unable to register those events.

    Source Code is as follows:-
    XAML

    <

     

    Page.Resources>
    .
    .

     

     

     

    ..    <Style TargetType="telerikGrid:GridViewRow" x:Key="Grid1ItemStyle">

     

     

     

            <Setter Property="dragDrop:RadDragAndDropManager.AllowDrop" Value="True" />

     

     

     

            <Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />

     

     

     

        </Style>
        <Style TargetType="telerikGrid:GridViewRow" x:Key="Grid2ItemStyle">

     

     

     

            <Setter Property="dragDrop:RadDragAndDropManager.AllowDrop" Value="True" />

     

     

     

     

     

     

            <Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />

     

     

     

     

     

     

        </Style>

        <

     

    DataTemplate x:Key="ProductTemplate">

     

     

     

     

     

     

            <DockPanel Margin="2" MaxWidth="200">

     

     

     

     

     

     

                <TextBlock Text="{Binding TableName}" FontWeight="Bold" DockPanel.Dock="Top" />

     

     

     

     

     

     

                <TextBlock Text="{Binding ColumnName}" Foreground="Green" DockPanel.Dock="Left" />

     

     

     

     

     

     

                <TextBlock Text="{Binding ColumnFriendlyName}" DockPanel.Dock="Left" Margin="2 0 0 0" Foreground="Gray" TextWrapping="Wrap" />

     

     

     

     

     

     

            </DockPanel>

     

     

     

     

     

     

        </DataTemplate>

     

     

     

     

     

     

        <DataTemplate x:Key="Product1Template">

     

     

     

     

     

     

            <DockPanel Margin="2" MaxWidth="200">

     

     

     

     

     

     

                <TextBlock Text="{Binding Table1Name}" FontWeight="Bold" DockPanel.Dock="Top" />

     

     

     

     

     

     

                <TextBlock Text="{Binding Column1Name}" Foreground="Green" DockPanel.Dock="Left" />

     

     

     

     

     

     

            </DockPanel>

     

     

     

     

     

     

        </DataTemplate>

     

     

     

     

     

    .
    .
    </
    Page.Resources>

     

     

     

    <Grid>

     

    .
    .
    .

     

    <

     

    telerik:RadGridView x:Name="gridSourceTableColumns" Margin="10,0,8,0" AutoGenerateColumns="False" ShowGroupPanel="False"

     

     

     

    HorizontalAlignment="Stretch" CanUserFreezeColumns="False" telerik:StyleManager.Theme="Windows7" CanUserReorderColumns="False" ScrollMode="RealTime" Height="300" CanUserSortColumns="False" Width="Auto" IsReadOnly="False" RowIndicatorVisibility="Collapsed" Grid.ColumnSpan="2" SelectionMode="Multiple" Grid.Row="0" Grid.Column="0"  Loaded="gridSourceTableColumns_Loaded" CanUserInsertRows="True" dragDrop:RadDragAndDropManager.AllowDrag="True" RowStyle="{StaticResource Grid1ItemStyle}" >

     

     

     

    .
    .
    .

     

    </

     

    telerik:RadGridView>

     

     

     

    .
    .
    .

     

    <

     

    telerikNavigation:RadTabControl x:Name="tabDEEParameters" Grid.Row="2" Grid.Column="0" Height="250">

     

     

     

     

     

     

        <telerikNavigation:RadTabItem Header="Where Clause" x:Name="tabItemWhereClause">

     

     

     

     

     

     

            <telerik:RadGridView x:Name="grdSrcWhereClause" Margin="10,0,8,0" AutoGenerateColumns="False" ShowGroupPanel="False"

     

     

                HorizontalAlignment="Stretch" CanUserFreezeColumns="False"  CanUserSortColumns="False" ItemsSource="{Binding}"

     

     

                IsReadOnly="False" RowIndicatorVisibility="Collapsed" ScrollMode="RealTime" Width="Auto" IsFilteringAllowed="False"

     

     

                Grid.Row="1" Grid.ColumnSpan="2" Height="200" RowEditEnded="OnRowEditEnded" CanUserDeleteRows="True"

     

     

     

     

     

                CanUserInsertRows="True" RowStyle="{StaticResource Grid2ItemStyle}"

     

     

     

                dragDrop:RadDragAndDropManager.AllowDrop="True" CanUserReorderColumns="False"  >

     

     

     

     

     

     

    .
    .
    .

     

        </

     

    telerik:RadGridView>

     

     

     

     

    </

     

    telerikNavigation:RadTabItem>

     

     

     

     

    <

     

    telerikNavigation:RadTabItem Header="Join Parameters" x:Name="tabItemJoinParameters">

     

     

     

     

     

     

        <telerik:RadGridView x:Name="grdSrcJoinParameter" Margin="10,0,8,0" AutoGenerateColumns="False" ShowGroupPanel="False"

     

     

            HorizontalAlignment="Stretch" CanUserFreezeColumns="False" CanUserReorderColumns="False"

     

     

            CanUserSortColumns="False" Width="Auto" ItemsSource="{Binding}"

     

     

            IsReadOnly="False" RowIndicatorVisibility="Collapsed" ScrollMode="RealTime"

     

     

            Grid.Row="1" Grid.ColumnSpan="2" Height="200" RowEditEnded="OnRowEditEnded"

     

     

     

     

     

            CanUserInsertRows="True" CanUserDeleteRows="True" IsFilteringAllowed="False"

     

     

     

     

     

            dragDrop:RadDragAndDropManager.AllowDrop="True" AllowDrop="True"

     

     

     

     

     

            RowStyle="{StaticResource Grid2ItemStyle}">

     

     

     

     

     

     

    .
    .
    .
    .
    .
            
    </telerik:RadGridView>

     

     

     

     

     

     

    </telerikNavigation:RadTabItem>

     

     

     

    </telerikNavigation:RadTabControl>

     

     

     

    .
    .
    .
    .
    .
    </

     

    Grid>

     



    C# Code is as follows:-

    Constructor:-..... Initialise...

    public

     

    SAPSourceTableDetails()

     

    {

     

       // Allow dropping into the GridView only if the drop items:

     

     

        RadDragAndDropManager.AddDragQueryHandler(gridSourceTableColumns, OnDragQuery);

     

     

        // Change the dropping cue and choose an action for the sucessful drop in the GridView:

     

     

        RadDragAndDropManager.AddDragInfoHandler(gridSourceTableColumns, OnGridViewDragInfo);

     

     

     

        // Allow dropping into the GridView only if the dragged items:

     

     

        RadDragAndDropManager.AddDropQueryHandler(grdSrcWhereClause, OnDropQuery);

     

     

        // Change the dropping cue and choose an action for the sucessful drag in the GridView:

     

     

        RadDragAndDropManager.AddDropInfoHandler(grdSrcWhereClause, OnGridViewDropInfo);

     

     

     

         

    // Allow dropping into the GridView only if the drop items:

     

     

        RadDragAndDropManager.AddDragQueryHandler(grdSrcJoinParameter, OnDropJoinQuery);

     

     

        // Change the dropping cue and choose an action for the sucessful drop in the GridView:

     

     

        RadDragAndDropManager.AddDragInfoHandler(grdSrcJoinParameter, OnGridViewDropJoinInfo);

     

     

     

     

        //Add class handler to handle dropping before / after items:

     

     

        EventManager.RegisterClassHandler(typeof(GridViewRow), RadDragAndDropManager.DropQueryEvent, new     
        EventHandler
    <DragDropQueryEventArgs>(OnRowDropQuery));

     

     

        EventManager.RegisterClassHandler(typeof(GridViewRow), RadDragAndDropManager.DropInfoEvent, new     
        EventHandler
    <DragDropEventArgs>(OnRowDropInfo));

     



     

        // Make the GridView a drag Source:

     

     

        RadDragAndDropManager.SetAllowDrag(gridSourceTableColumns, true);

     

     

     

        // Make the GridView a drop target:

     

     

     

        RadDragAndDropManager.SetAllowDrop(grdSrcWhereClause, true);

     

     

        // Make the GridView a drag Source:

     

     

     

        RadDragAndDropManager.SetAllowDrop(grdSrcJoinParameter, true);

     

        tabDEEParameters.SelectedIndex = 2;


    }
    ...

    private

     

    DropPosition lastRowDropPosition;

     

     


     

    // Get the drag cue that we have created

     

     

     

    private

     

    void OnRowDropInfo(object sender, DragDropEventArgs e)

     

    {

     

    var row = sender as GridViewRow;

     

     

    var gridView = row.GridViewDataControl as RadGridView;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    // Get the drag cue that we have created

     

     

    var cue = e.Options.DragCue as TreeViewDragCue;

     

     

     

    if (e.Options.Status == DragStatus.DropPossible)

     

    {

     

    // Set a suitable text:

     

    cue.DragActionContent =

    String.Format("Insert {0} item{1} {2} ", draggedItems.Count, draggedItems.Count > 1 ? "s" : String.Empty, lastRowDropPosition.ToString().ToLower());

     

    cue.DragTooltipContent = row.Item;

    cue.DragTooltipContentTemplate =

    this.Resources["ProductTemplate"] as DataTemplate;

     

    grdSrcWhereClause.SetIsSelected(grdSrcWhereClause.SelectedItem,

    true);

     

    ShowBetweenItemsCue(gridView, row, lastRowDropPosition);

     

    //We need to think of some kind of template here, we will use one we have.

     

    cue.IsDropPossible =

    true;

     

    }

     

    else if (e.Options.Status == DragStatus.DropImpossible)

     

    {

    HideBetweenItemsCue(gridView);

    cue.DragActionContent =

    null;

     

    cue.IsDropPossible =

    false;

     

    }

     

    else if (e.Options.Status == DragStatus.DropComplete)

     

    {

    HideBetweenItemsCue(gridView);

     

    var items = gridView.ItemsSource as IList;

     

     

    var insertIndex = items.IndexOf(row.Item);

     

     

    if (lastRowDropPosition == DropPosition.After)

     

    {

    insertIndex++;

    }

     

    foreach (var draggedItem in draggedItems)

     

    {

    items.Insert(insertIndex, draggedItem);

    insertIndex++;

    }

    }

    e.Handled =

    true;

     

    }

     

    private void OnRowDropJoinInfo(object sender, DragDropEventArgs e)

     

    {

     

    var row = sender as GridViewRow;

     

     

    var gridView = row.GridViewDataControl as RadGridView;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    // Get the drag cu that the TreeView or we have created

     

     

    var cue = e.Options.DragCue as TreeViewDragCue;

     

     

     

    if (e.Options.Status == DragStatus.DropPossible)

     

    {

     

    // Set a suitable text:

     

    cue.DragActionContent =

    String.Format("Insert {0} item{1} {2} ", draggedItems.Count, draggedItems.Count > 1 ? "s" : String.Empty, lastRowDropPosition.ToString().ToLower());

     

    cue.DragTooltipContent = row.Item;

    cue.DragTooltipContentTemplate =

    this.Resources["Product1Template"] as DataTemplate;

     

    grdSrcJoinParameter.SetIsSelected(grdSrcWhereClause.SelectedItem,

    true);

     

    ShowBetweenItemsCue(gridView, row, lastRowDropPosition);

     

    //We need to think of some kind of template here, we will use one we have.

     

    cue.IsDropPossible =

    true;

     

    }

     

    else if (e.Options.Status == DragStatus.DropImpossible)

     

    {

    HideBetweenItemsCue(gridView);

    cue.DragActionContent =

    null;

     

    cue.IsDropPossible =

    false;

     

    }

     

    else if (e.Options.Status == DragStatus.DropComplete)

     

    {

    HideBetweenItemsCue(gridView);

     

    var items = gridView.ItemsSource as IList;

     

     

    var insertIndex = items.IndexOf(row.Item);

     

     

    if (lastRowDropPosition == DropPosition.After)

     

    {

    insertIndex++;

    }

     

    foreach (var draggedItem in draggedItems)

     

    {

    items.Insert(insertIndex, draggedItem);

    insertIndex++;

    }

    }

    e.Handled =

    true;

     

    }

     

     

    private void ShowBetweenItemsCue(RadGridView gridView, GridViewRow row, DropPosition lastRowDropPosition)

     

    {

     

    if (gridView != null)

     

    {

     

    //We need to get the first visual child so that we can search in the template:

     

     

    var firstChild = VisualTreeHelper.GetChild(gridView, 0) as FrameworkElement;

     

     

    var cue = firstChild.FindName("BetweenItemsCue") as FrameworkElement;

     

    cue.Visibility =

    Visibility.Visible;

     

     

    //How much should the cue be offset?

     

     

    var yOffset = row.TransformToVisual(gridView).Transform(new Point()).Y;

     

     

    if (lastRowDropPosition == DropPosition.After)

     

    {

    yOffset += row.ActualHeight;

    }

    cue.RenderTransform =

    new TranslateTransform() { Y = yOffset };

     

    }

    }

     

    private void HideBetweenItemsCue(RadGridView gridView)

     

    {

     

    if (gridView != null)

     

    {

     

    //We need to get the first visual child so that we can search in the template:

     

     

    var firstChild = VisualTreeHelper.GetChild(gridView, 0) as FrameworkElement;

     

     

    var cue = firstChild.FindName("BetweenItemsCue") as FrameworkElement;

     

    cue.Visibility =

    Visibility.Collapsed;

     

    }

    }

     

    private void OnRowDropJoinQuery(object sender, DragDropQueryEventArgs e)

     

    {

     

    var row = sender as GridViewRow;

     

     

    var dragCue = e.Options.DragCue as TreeViewDragCue;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    //Get the Y displacement ragarding the screen:

     

     

    var top = row.TransformToVisual(this).Transform(new Point()).Y;

     

     

    var currentPoint = e.Options.CurrentDragPoint;

     

    lastRowDropPosition = currentPoint.Y - top < row.ActualHeight / 2 ?

    DropPosition.Before : DropPosition.After;

     

     

    //Here you can check whether a drop should really be allowed.

     

    e.QueryResult = draggedItems.Cast<

    object>().All(item => item is SAPTable);

     

    e.Handled =

    true;

     

    }

     

    private void OnRowDropQuery(object sender, DragDropQueryEventArgs e)

     

    {

     

    var row = sender as GridViewRow;

     

     

    var dragCue = e.Options.DragCue as TreeViewDragCue;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    //Get the Y displacement ragarding the screen:

     

     

    var top = row.TransformToVisual(this).Transform(new Point()).Y;

     

     

    var currentPoint = e.Options.CurrentDragPoint;

     

    lastRowDropPosition = currentPoint.Y - top < row.ActualHeight / 2 ?

    DropPosition.Before : DropPosition.After;

     

     

    if (draggedItems != null)

     

    {

     

    //Here you can check whether a drop should really be allowed.

     

    e.QueryResult = draggedItems.Cast<

    object>().All(item => item is SAPTable);

     

    }

    e.Handled =

    true;

     

    }

     

    private void OnGridViewDragInfo(object sender, DragDropEventArgs e)

     

    {

     

    var gridView = sender as RadGridView;

     

     

    var draggedItems = e.Options.Payload as IEnumerable;

     

     

    if (e.Options.Status == DragStatus.DragInProgress)

     

    {

     

    this.Dispatcher.BeginInvoke(new Action(() =>

     

    {

     

    var capturedElement = Mouse.Captured;

     

     

    if (capturedElement != null)

     

    {

    capturedElement.ReleaseMouseCapture();

    }

    }));

     

    //Set up a drag cue:

     

     

    var cue = new TreeViewDragCue();

     

     

    if (e.Options.Payload != null)

     

    {

     

    //Here we need to choose a template for the items:

     

     

    if (e.Options.Payload.GetType().Name == "SAPJoinClause")

     

    {

    cue.ItemTemplate =

    this.Resources["Product1Template"] as DataTemplate;

     

    }

     

    else

     

    {

    cue.ItemTemplate =

    this.Resources["ProductTemplate"] as DataTemplate;

     

    }

    }

    cue.ItemsSource = draggedItems;

    e.Options.DragCue = cue;

    }

     

    //else if (e.Options.Status == DragStatus.DragComplete)

     

     

    //{

     

     

    // var source = gridView.ItemsSource as IList;

     

     

    // foreach (object draggedItem in draggedItems)

     

     

    // {

     

     

    // source.Remove(draggedItem);

     

     

    // }

     

     

    //}

     

    }

     

    private void OnDragQuery(object sender, DragDropQueryEventArgs e)

     

    {

     

    var gridView = sender as RadGridView;

     

     

    if (gridView != null)

     

    {

     

    var selectedItems = gridView.SelectedItems.ToList();

     

    e.QueryResult = selectedItems.Count > 0;

     

    SAPWhereClause clause = new SAPWhereClause();

     

     

    SAPJoinClause clause1 = new SAPJoinClause();

     

     

    foreach (SAPSourceFieldInfo item in selectedItems)

     

    {

     

    if (tabDEEParameters.SelectedIndex == 2)

     

    {

     

    SAPJoinCond table = new SAPJoinCond(item.PI_ERPTableID, -1, item.TableName, "", item.PI_ERPColumnID, -1, item.ColumnName,

     

     

    "", "", "", "", "", -1, "");

     

    clause1.Add(table);

    }

     

    else if (tabDEEParameters.SelectedIndex == 1)

     

    {

     

    SAPWhereCond table = new SAPWhereCond(item.PI_ERPTableID, item.TableName, item.PI_ERPColumnID,

     

    item.ColumnName, -1,

    "", "", "", "", "", "", "");

     

    clause.Add(table);

    }

    }

     

    if (tabDEEParameters.SelectedIndex == 2)

     

    {

    e.Options.Payload = clause1;

    }

     

    else if (tabDEEParameters.SelectedIndex == 1)

     

    {

    e.Options.Payload = clause;

    }

     

    }

    e.QueryResult =

    true;

     

    e.Handled =

    true;

     

    }

     

    private void OnGridViewDropJoinInfo(object sender, DragDropEventArgs e)

     

    {

     

    var destGrid = e.Options.Destination as RadGridView;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    // Get the drag cu that the TreeView or we have created

     

     

    var cue = e.Options.DragCue as TreeViewDragCue;

     

     

     

    if (e.Options.Status == DragStatus.DropPossible)

     

    {

     

    // Set a suitable text:

     

    cue.DragActionContent =

    String.Format("Add {0} item{1} to Join Parameters", draggedItems.Count, draggedItems.Count > 1 ? "s" : String.Empty);

     

    cue.IsDropPossible =

    true;

     

     

    if (destGrid != null)

     

    {

    destGrid.Background =

    this.Resources["DropPossibleBackground"] as Brush;

     

    }

    }

     

    else if (e.Options.Status == DragStatus.DropImpossible)

     

    {

    cue.DragActionContent =

    null;

     

    cue.IsDropPossible =

    false;

     

    }

     

    else if (e.Options.Status == DragStatus.DropComplete)

     

    {

     

    var items = grdSrcJoinParameter.ItemsSource as IList;

     

     

    foreach (var draggedItem in draggedItems)

     

    {

    items.Add(draggedItem);

    }

    grdSrcWhereClause.SelectedItem =

    null;

     

    grdSrcWhereClause.Rebind();

    }

     

    if (e.Options.Status != DragStatus.DropPossible)

     

    {

     

    if (destGrid != null)

     

    {

    destGrid.Background =

    new SolidColorBrush(Colors.White);

     

    }

    }

     

    foreach (var parent in e.Options.Source.GetAncestorsInclusive<UIElement>())

     

    {

    parent.ReleaseMouseCapture();

    }

    }

     

    private void OnGridViewDropInfo(object sender, DragDropEventArgs e)

     

    {

     

    var destGrid = e.Options.Destination as RadGridView;

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    // Get the drag cu that the TreeView or we have created

     

     

    var cue = e.Options.DragCue as TreeViewDragCue;

     

     

     

    if (e.Options.Status == DragStatus.DropPossible)

     

    {

     

    // Set a suitable text:

     

    cue.DragActionContent =

    String.Format("Add {0} item{1} to Conditions", draggedItems.Count, draggedItems.Count > 1 ? "s" : String.Empty);

     

    cue.IsDropPossible =

    true;

     

     

    if (destGrid != null)

     

    {

    destGrid.Background =

    this.Resources["DropPossibleBackground"] as Brush;

     

    }

    }

     

    else if (e.Options.Status == DragStatus.DropImpossible)

     

    {

    cue.DragActionContent =

    null;

     

    cue.IsDropPossible =

    false;

     

    }

     

    else if (e.Options.Status == DragStatus.DropComplete)

     

    {

     

    var items = grdSrcWhereClause.ItemsSource as IList;

     

     

    foreach (var draggedItem in draggedItems)

     

    {

    items.Add(draggedItem);

    }

    grdSrcWhereClause.SelectedItem =

    null;

     

    grdSrcWhereClause.Rebind();

    }

     

    if (e.Options.Status != DragStatus.DropPossible)

     

    {

     

    if (destGrid != null)

     

    {

    destGrid.Background =

    new SolidColorBrush(Colors.White);

     

    }

    }

     

    foreach (var parent in e.Options.Source.GetAncestorsInclusive<UIElement>())

     

    {

    parent.ReleaseMouseCapture();

    }

    }

     

    private void OnDropQuery(object sender, DragDropQueryEventArgs e)

     

    {

     

    // We allow drop only if the dragged items are products:

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    var result = draggedItems.Cast<object>().All(item => item is SAPWhereCond);

     

    e.QueryResult = result;

    e.Handled =

    true;

     

     

    // Note that here we agree to accept a drop. We will be notified

     

     

    // in the DropInfo event whether a drop is actually possible.

     

    }

     

    private void OnDropJoinQuery(object sender, DragDropQueryEventArgs e)

     

    {

     

    // We allow drop only if the dragged items are products:

     

     

    var draggedItems = e.Options.Payload as ICollection;

     

     

    var result = draggedItems.Cast<object>().All(item => item is SAPJoinCond);

     

    e.QueryResult = result;

    e.Handled =

    true;

     

     

    // Note that here we agree to accept a drop. We will be notified

     

     

    // in the DropInfo event whether a drop is actually possible.

     

    }


    Thanks for your help...

    Neha

  2. neha
    neha avatar
    5 posts
    Member since:
    May 2010

    Posted 28 Jul 2010 Link to this post

    Thanks for your help.. I got this working.. events were not registered for the DROP on the 2nd grid...
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top