Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


<pre lang="sql">
CREATE TABLE MyTable 
(IdTable Int(2), 
IdLabel Int(20), 
LName char(50) 
)

<pre lang="sql">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; } 
    } 
  
    
      
} 


Goldoni
Posted 33 minutes ago (permalink)
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
<pre lang="xml"><Window x:Class="ProfilTandem.WindowTreeView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        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
Posted

Just run with the debugger ans see. It looks like either radGridView.ItemsSource is null or its type does not implement the interface IList<Label> which would return null result of the as operator. Check where you assign ItemsSource and its type. In your code, don't make implicit assumptions you made in this line.

—SA
 
Share this answer
 
v2
Comments
Member 3885129 12-Jul-11 12:19pm    
thank you for reply but i don't understand.

i assign ItemsSource in the behind code with:
<pre>radGridView.ItemsSource = dataProvider.Get_Table();</pre>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900