Trouble with a drag and drop TreeView and Gridview

3 posts, 0 answers
  1. Goldoni
    Goldoni avatar
    2 posts
    Member since:
    Jun 2011

    Posted 12 Jul 2011 Link to this post

    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

    (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()
                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();
                // 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"
            Title="MainWindow" Height="450" Width="585">
            <DataTemplate x:Key="Team">
                <TextBlock Text="{Binding  Path=LName}"/>
            <HierarchicalDataTemplate DataType="{x:Type northwind:Label}" ItemsSource="{Binding Path=LName}">
                <Border Width="250">
                     Text="{Binding Path=IdLabel}" FontSize="14" FontWeight="Bold" Margin="2" >
            <HierarchicalDataTemplate DataType="{x:Type northwind:GroupLabel}">
                <Grid Margin="6">
                    <TextBlock Text="{Binding Path=LName}" Padding="2" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto" />
            <telerik:RadTreeView IsDragDropEnabled="True" Grid.Column="0" Grid.Row="1"
                   SelectionMode="Multiple"      IsEditable="True"  x:Name="treeView1" Margin="0,20,0,245"
          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:GridViewDataColumn DataMemberBinding="{Binding Path=IdLabel}"/>
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=LName}"/>

    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. Ivan Ivanov
    Ivan Ivanov avatar
    1116 posts

    Posted 13 Jul 2011 Link to this post

    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!

  3. DevCraft banner
  4. Goldoni
    Goldoni avatar
    2 posts
    Member since:
    Jun 2011

    Posted 19 Jul 2011 Link to this post

    Thank you
Back to Top