This is a migrated thread and some comments may be shown as answers.

Trouble with a drag and drop TreeView and Gridview

2 Answers 99 Views
DragAndDrop
This is a migrated thread and some comments may be shown as answers.
Goldoni
Top achievements
Rank 1
Goldoni asked on 12 Jul 2011, 03:36 PM
Good morning,

I would like to use a TreeView  that's can drag and drop in  a GridView

I make the TreeView but when a want to drag and drop i have a probleme the null Exception, after   4 days i can't see the problem.

Let me show you my code to help me please.

I have a class library  LabelData  to make the database and a  data class 
 
this is my table


CREATE TABLE MyTable
(IdTable Int(2),
IdLabel Int(20),
LName char(50)
)
and my  Rows
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (1, 200, 'firstName');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (2, 200, 'lastName');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (3, 200, 'middleName');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (4, 201, 'circuit');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (5, 201, 'series');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (6, 202, 'country');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (7, 202, 'region');
  
INSERT INTO MyTable (IdTable,IdLabel,LName) VALUES (8, 202, 'zipcode');

For make the treeview, I use  IdLabel to make like "categorie"  and i  use LName to make like "product" .

I add a heritance in on my table MyTable   GroupLabel  with the property LName


This is my class  data.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
LNamespace LabelData
{
    public class DataProvider
    {
        LabelDataContext dc;
  
        public DataProvider()
        {
            dc = new LabelDataContext();
        }
  
        public IEnumerable<Label> GetLabel()
        {
            return from e in dc.MyTable
                   group e by e.IdLabel into gr
                   select new Label() { IdLabel = gr.Key, LName= gr };
              
        }
  
        public IEnumerable<MyTable> Get_Table()
        {
            return from p in dc.MyTable
                   select p;
  
        }
  
  
      
     }
  
    public class Label
    {
        public int IdLabel { get; set; }
        public IEnumerable<MyTable> LName{ get; set; }
    }
  
    
      
}


And this is my behind code xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls.DragDrop;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using LabelData;
using Telerik.Windows.Controls.TreeView;
using System.Collections;
  
using System.Net;
  
using System.Windows.Media.Animation;
  
  
using Telerik.Windows;
  
  
LNamespace LabelMunich
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class WindowTreeView : Window
    {
        public WindowTreeView()
        {
            InitializeComponent();
            ProfilData.DataProvider dataProvider = new ProfilData.DataProvider();
            treeView1.ItemsSource = dataProvider.GetModule();
            radGridView.ItemsSource = dataProvider.Get_Table();
  
            RadDragAndDropManager.AddDropQueryHandler(radGridView, new EventHandler<DragDropQueryEventArgs>(this.RadGridView_OnDropQuery));
            RadDragAndDropManager.AddDropInfoHandler(radGridView, new EventHandler<DragDropEventArgs>(this.RadGridView_OnDropInfo)); 
  
  
        }
        private void RadRibbonButton_Click(object sender, RoutedEventArgs e)
        {
            // FAjout fa = new FAjout();
            formAjout fa = new formAjout();
            fa.Show();
            // this.ShowActivated();
        }
  
        private void radTreeView_PreviewDragEnded(object sender, RadTreeViewDragEndedEventArgs e)
        {
            e.Handled = true;
        }
  
  
        private void RadGridView_OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            e.QueryResult = (e.Options.Source as RadTreeViewItem).Item is Module;
        }
        private void RadGridView_OnDropInfo(object sender, DragDropEventArgs e)
        {
            if (e.Options.Status == DragStatus.DropComplete)
            {
                (radGridView.ItemsSource as IList<Module>).Add((e.Options.Payload as Collection<Object>)[0] as Module);
            }
        
       
    }
}

And my Xaml document

<Window x:Class="ProfilTandem.WindowTreeView"
        xmlns:northwind="clr-namespace:ProfilData;assembly=LabelData"
        xmlns:local="clr-namespace:LabelMunich"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"    
        xmlns:telerikDragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls" 
        Title="MainWindow" Height="450" Width="585">
    <Window.Resources>
        <DataTemplate x:Key="Team">
            <TextBlock Text="{Binding  Path=LName}"/>
        </DataTemplate>
  
        <HierarchicalDataTemplate DataType="{x:Type northwind:Label}" ItemsSource="{Binding Path=LName}">
            <Border Width="250">
                <Grid>
                    <TextBlock  
                 Text="{Binding Path=IdLabel}" FontSize="14" FontWeight="Bold" Margin="2" >
                    </TextBlock>
                </Grid>
            </Border>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type northwind:GroupLabel}">
            <Grid Margin="6">
                <TextBlock Text="{Binding Path=LName}" Padding="2" />
            </Grid>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>       
          
        <telerik:RadTreeView IsDragDropEnabled="True" Grid.Column="0" Grid.Row="1"
               SelectionMode="Multiple"      IsEditable="True"  x:Name="treeView1" Margin="0,20,0,245"
                PreviewDragEnded="radTreeView_PreviewDragEnded"
      IsDragTooltipEnabled="False" ItemsSource="{Binding}"/>
  
        <StackPanel Grid.Column="1" Grid.Row="1">
            <telerik:RadGridView x:Name="radGridView"  AutoGenerateColumns="False"
          Width="400" Height="400" HorizontalAlignment="Left" telerikDragDrop:RadDragAndDropManager.AllowDrop="True" 
          DataContext="{Binding}"   ItemsSource="{Binding}">
                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=IdLabel}"/>
                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=LName}"/>
  
                    </telerik:RadGridView.Columns>
            </telerik:RadGridView>
        </StackPanel>
  
    </Grid>
</Window>

This is all


When i try to drag items treeview to gridview i have exception on this line 

  (radGridView.ItemsSource as IList<Label>).Add((e.Options.Payload as Collection<Object>)[0] as Label);

The exception System.NullReferenceException was unhandled

Object reference not set to an instance of an object

Thank you for your help

2 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 13 Jul 2011, 04:21 PM
Hello Goldoni,

Fortunately we have an example that deals with a similar scenario. It can be found here: examples -> Drag and drop -> Tree to Grid. I believe that it will prove useful for your case. Please, do not hesitate to contact us if any further inquires occur.

Kind regards,
Ivan Ivanov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Goldoni
Top achievements
Rank 1
answered on 19 Jul 2011, 02:38 PM
Thank you
Tags
DragAndDrop
Asked by
Goldoni
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Goldoni
Top achievements
Rank 1
Share this question
or