Hello,
I am implementing drag and drop functionality between gridviews using radDragAndDrop. Users are moved from one gridview to the other to be included in the group. I was having a ArgumentOutOfRangeException after performing the drag and drop. After a while I found out that cleaning the grid.SelectedItem, the error disappeared, but then the grid selection behaves in a very strange way.
When the mouse gets over the grid, and then moves over another row, all the rows between the first one and the row where the mouse is, get selected. This is a strange behaviour due probably to the fact that I had to clean the selected rows after the drag / drop has been completed to avoid the ArgumentOutOfRangeException. The line "grid.SelectedItem = null;" at the end of the methods
OnDropInfo and OnDragInfo stops the exception but starts the strange selection behaviour.
Thanks is advance,
Pablo
The exception that I am having is:
Microsoft JScript runtime error: Unhandled Error in Silverlight 2 Application L'index était hors limites. Il ne doit pas être négatif et doit être inférieur à la taille de la collection.
Nom du paramètre : index à System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
à System.ThrowHelper.ThrowArgumentOutOfRangeException()
à System.Collections.Generic.List`1.get_Item(Int32 index)
à Telerik.Windows.Data.Grouping.DataRecordList`1.get_Item(Int32 index)
à Telerik.Windows.Data.VirtualizingRecordCollection.get_Item(Int32 index)
à Telerik.Windows.Data.Selection.SelectionHandler.SameGroupSelection(IList`1 collection, Record actionItem)
à Telerik.Windows.Data.Selection.SelectionHandler.SelectRange(Record actionItem, Boolean cleanCurrentSelection)
à Telerik.Windows.Data.Selection.SelectionHandler.PerformMultipleSelection(Record record, ModifierKeys modifierKeys)
à Telerik.Windows.Data.Selection.SelectionHandler.PerformSelection(Record record, ModifierKeys modifierKeys)
à Telerik.Windows.Controls.SelectionManager.TrySelectRow(RadRowItem radRowItem)
à Telerik.Windows.Controls.SelectionManager.RadRowItem_RowMouseEnter(Object sender, RowItemMouseEventArgs e)
The xaml file is:
<
UserControl x:Class="Elia.AuthorizationViewer.Client.Pages.RadDragGridView"
xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:GridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
xmlns:dragDrop="clr-namespace:Telerik.Windows.Controls.DragDrop;assembly=Telerik.Windows.Controls"
xmlns:pages="clr-namespace:Client.Pages">
<UserControl.Resources>
<Style x:Key="TextBlock" TargetType="TextBlock">
<Setter Property="Margin" Value="5"/>
</Style>
<Style x:Key="SmallButton" TargetType="Button">
<Setter Property="Margin" Value="3" />
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid x:Name="grdGroupMng" >
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="25"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="All users" Grid.Column="0" Grid.Row="0" Style="{StaticResource TextBlock}"></TextBlock>
<TextBlock Text="Users in group" Grid.Column="2" Grid.Row="0" Style="{StaticResource TextBlock}"></TextBlock>
<pages:DraggableGridViewTest x:Name="rgvAllUsers" Grid.Column="0" Grid.Row="1" IsReadOnly="True" AutoGenerateColumns="False"
ScrollMode="RealTime" ShowGroupPanel="False" Height="400" BorderThickness="1" MultipleSelect="True" >
<telerikGridView:RadGridView.Columns>
<telerikGridView:GridViewDataColumn IsFilterable="True" DataMemberPath="LastName" HeaderText="Name" Width="*" >
<telerikGridView:GridViewDataColumn.CellStyle>
<Style TargetType="GridView:GridViewCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridView:GridViewCell">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding LastName}" Margin="2" VerticalAlignment="Center"></TextBlock>
<TextBlock Text=" "></TextBlock>
<TextBlock Text="{Binding FirstName}" Margin="2" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
</Style>
</telerikGridView:GridViewDataColumn.CellStyle>
</telerikGridView:GridViewDataColumn>
<telerikGridView:GridViewDataColumn DataMemberPath="Login" HeaderText="Login" Width="200"></telerikGridView:GridViewDataColumn>
</telerikGridView:RadGridView.Columns>
<telerikGridView:RadGridView.RowAppearance>
<telerikGridView:RowAppearanceSettings>
<telerikGridView:RowAppearanceSettings.RowStyle>
<Style TargetType="GridView:GridViewRow">
<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />
</Style>
</telerikGridView:RowAppearanceSettings.RowStyle>
<telerikGridView:RowAppearanceSettings.AlternateRowStyle>
<Style TargetType="GridView:GridViewRow">
<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />
</Style>
</telerikGridView:RowAppearanceSettings.AlternateRowStyle>
</telerikGridView:RowAppearanceSettings>
</telerikGridView:RadGridView.RowAppearance>
</pages:DraggableGridViewTest>
<pages:DraggableGridViewTest x:Name="rgvUsersInGroup" Grid.Column="2" Grid.Row="1" IsReadOnly="True" AutoGenerateColumns="False"
ScrollMode="RealTime" ShowGroupPanel="False" Height="400" MultipleSelect="True" >
<telerikGridView:RadGridView.Columns>
<telerikGridView:GridViewDataColumn IsFilterable="True" DataMemberPath="LastName" HeaderText="Name" Width="*" >
<telerikGridView:GridViewDataColumn.CellStyle>
<Style TargetType="GridView:GridViewCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GridView:GridViewCell">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}">
<StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock Text="{Binding LastName}" Margin="2" VerticalAlignment="Center"></TextBlock>
<TextBlock Text=" "></TextBlock>
<TextBlock Text="{Binding FirstName}" Margin="2" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="0,0,1,1"/>
</Style>
</telerikGridView:GridViewDataColumn.CellStyle>
</telerikGridView:GridViewDataColumn>
<telerikGridView:GridViewDataColumn DataMemberPath="Login" HeaderText="Login" Width="150"></telerikGridView:GridViewDataColumn>
</telerikGridView:RadGridView.Columns>
<telerikGridView:RadGridView.RowAppearance>
<telerikGridView:RowAppearanceSettings>
<telerikGridView:RowAppearanceSettings.RowStyle>
<Style TargetType="GridView:GridViewRow">
<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />
</Style>
</telerikGridView:RowAppearanceSettings.RowStyle>
<telerikGridView:RowAppearanceSettings.AlternateRowStyle>
<Style TargetType="GridView:GridViewRow">
<Setter Property="dragDrop:RadDragAndDropManager.AllowDrag" Value="True" />
</Style>
</telerikGridView:RowAppearanceSettings.AlternateRowStyle>
</telerikGridView:RowAppearanceSettings>
</telerikGridView:RadGridView.RowAppearance>
</pages:DraggableGridViewTest>
<StackPanel x:Name="ButtonActions" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Stretch" Grid.Column="1" Grid.Row="1">
<Button x:Name="btnAddToGroup" Content=">" Style="{StaticResource SmallButton}" IsEnabled="True"></Button>
<Button x:Name="btnRemoveFromGroup" Content="<" Style="{StaticResource SmallButton}" IsEnabled="True"></Button>
</StackPanel>
<Button x:Name="btnSaveGroup" Grid.Column="2" Grid.Row="2" Content="Save" Width="100" Height="25" Margin="5"
HorizontalAlignment="Right" IsEnabled="False"></Button>
</Grid>
</StackPanel>
</
UserControl>
The code behind is:
using
System;
using
System.Collections.Generic;
using
System.Windows.Controls;
using
System.Windows.Media;
using
Telerik.Windows;
using
Telerik.Windows.Controls;
using
Telerik.Windows.Controls.DragDrop;
namespace
Client.Pages
{
public partial class RadDragGridView : UserControl
{
public RadDragGridView()
{
InitializeComponent();
CreateData();
}
void CreateData()
{
List<TestUser> users = new List<TestUser>();
users.Add(
new TestUser { FirstName = "Peter", LastName = "LastName3", Login = "AZE123", });
users.Add(
new TestUser { FirstName = "Pablo", LastName = "LastName2", Login = "QSF456", });
users.Add(
new TestUser { FirstName = "John", LastName = "LastName", Login = "FHG456", });
users.Add(
new TestUser { FirstName = "Simon", LastName = "Kraus", Login = "GHJ546", });
rgvAllUsers.ItemsSource = users;
users =
new List<TestUser>();
users.Add(
new TestUser { FirstName = "Name1", LastName = "LastName3", Login = "rty123", });
users.Add(
new TestUser { FirstName = "Name2", LastName = "LastName2", Login = "fhh456", });
users.Add(
new TestUser { FirstName = "Name3", LastName = "LastName", Login = "dgf456", });
users.Add(
new TestUser { FirstName = "Name4", LastName = "Kraus", Login = "ghh546", });
rgvUsersInGroup.ItemsSource = users;
}
}
public class DraggableGridViewTest : RadGridView
{
Brush listBoxDefaultBrush;
Brush listBoxDragPossible = new SolidColorBrush(Colors.Orange);
public event EventHandler<DragDropQueryEventArgs> DragQuery
{
add
{
this.AddHandler(RadDragAndDropManager.DragQueryEvent, value);
}
remove
{
this.RemoveHandler(RadDragAndDropManager.DragQueryEvent, value);
}
}
public event EventHandler<DragDropQueryEventArgs> DropQuery
{
add
{
this.AddHandler(RadDragAndDropManager.DropQueryEvent, value);
}
remove
{
this.RemoveHandler(RadDragAndDropManager.DropQueryEvent, value);
}
}
public event EventHandler<DragDropEventArgs> DragInfo
{
add
{
this.AddHandler(RadDragAndDropManager.DragInfoEvent, value);
}
remove
{
this.RemoveHandler(RadDragAndDropManager.DragInfoEvent, value);
}
}
public event EventHandler<DragDropEventArgs> DropInfo
{
add
{
this.AddHandler(RadDragAndDropManager.DropInfoEvent, value);
}
remove
{
this.RemoveHandler(RadDragAndDropManager.DropInfoEvent, value);
}
}
public DraggableGridViewTest()
{
RadDragAndDropManager.SetAllowDrop(this, true);
this.DragQuery += new EventHandler<DragDropQueryEventArgs>(OnDragQuery);
this.DragInfo += new EventHandler<DragDropEventArgs>(OnDragInfo);
this.DropQuery += new EventHandler<DragDropQueryEventArgs>(OnDropQuery);
this.DropInfo += new EventHandler<DragDropEventArgs>(OnDropInfo);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
listBoxDefaultBrush = BorderBrush;
}
void OnDropInfo(object sender, DragDropEventArgs e)
{
DraggableGridViewTest grid = sender as DraggableGridViewTest;
IList<TestUser> itemsSource = grid.ItemsSource as IList<TestUser>;
List<TestUser> payload = e.Options.Payload as List<TestUser>;
if (e.Options.Status == DragStatus.DropPossible)
{
grid.BorderBrush = listBoxDragPossible;
}
else
{
grid.BorderBrush = listBoxDefaultBrush;
}
if (e.Options.Status == DragStatus.DropComplete)
{
foreach (var user in payload)
{
if (!itemsSource.Contains(user))
{
itemsSource.Add(user);
}
}
grid.ItemsSource =
new List<TestUser>();
grid.ItemsSource = itemsSource;
grid.SelectedItem =
null;
}
}
void OnDropQuery(object sender, DragDropQueryEventArgs e)
{
DraggableGridViewTest box = sender as DraggableGridViewTest;
IList<TestUser> itemsSource = box.ItemsSource as IList<TestUser>;
List<TestUser> payload = e.Options.Payload as List<TestUser>;
e.QueryResult =
false;
if (payload != null)
{
foreach (var user in payload)
{
if (!itemsSource.Contains(user))
{
e.QueryResult =
true;
break;
}
}
}
}
void OnDragInfo(object sender, DragDropEventArgs e)
{
DraggableGridViewTest grid = sender as DraggableGridViewTest;
IList<TestUser> itemsSource = grid.ItemsSource as IList<TestUser>;
List<TestUser> payload = e.Options.Payload as List<TestUser>;
if (e.Options.Status == DragStatus.DragComplete)
{
if (payload != null)
{
foreach (var user in payload)
{
if (itemsSource.Contains(user))
{
itemsSource.Remove(user);
}
}
grid.ItemsSource =
new List<TestUser>();
grid.ItemsSource = itemsSource;
grid.SelectedItem =
null;
}
}
}
protected virtual void OnDragQuery(object sender, DragDropQueryEventArgs e)
{
DraggableGridViewTest grid = sender as DraggableGridViewTest;
e.QueryResult =
true;
TestUser usr = null;
if (e.Options.Status == DragStatus.DragQuery)
{
e.Options.SourceCueHost =
null;
List<TestUser> users = new List<TestUser>();
string cueText = string.Empty;
foreach (var item in grid.SelectedRecords)
{
usr = ((Telerik.Windows.Data.
DataRecord) item).Data as TestUser;
users.Add(usr);
cueText += usr.LastName +
" " + usr.FirstName;
}
e.Options.Payload = users;
ContentControl cue = new ContentControl();
cue.Content = cueText;
e.Options.DragCue = cue;
e.Options.ArrowCue =
RadDragAndDropManager.GenerateArrowCue();
}
}
}
public class TestUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Login { get; set; }
}
}