Crash when copying cell value to row

I have an sfDataGrid bound to an underlying observable collection of objects.  If a user selects a cell, copies the value, then selects the row and hits <ctrl-v> to paste I get a crash.  This is using version 24.2.9.


Here's my xaml:-

<Window x:Class="testgridcopy.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:testgridcopy"

        xmlns:sf="http://schemas.syncfusion.com/wpf"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="*"/>

            <RowDefinition Height="Auto"/>

        </Grid.RowDefinitions>


        <sf:SfDataGrid ItemsSource="{Binding TestObjects}"

                AutoGenerateColumns="True" AutoGenerateColumnsMode="SmartReset"

                AllowResizingColumns="True" ColumnSizer="Auto"

                ShowRowHeader="True" SelectionMode="Multiple"

                AllowEditing="True" EditTrigger="OnTap"

                AllowDeleting="True" SelectionUnit="Row"

                Grid.Row="0" Margin="2,2,2,2"

                CurrentCellActivated="OnCurrentCellActivated"

                GridCopyOption="None">

            <sf:SfDataGrid.Columns>

                <sf:GridTemplateColumn sf:FocusManagerHelper.WantsKeyInput="True">

                    <sf:GridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <Button Content="x"

                                            sf:FocusManagerHelper.FocusedElement="True"

                                            sf:VisualContainer.WantsMouseInput="True"

                                            ClickMode="Press"

                                            Click="DeleteTestObject_Click"

                                            Width="Auto"

                                            ToolTip="Delete Test Object"

                                            Tag="{Binding}" />

                        </DataTemplate>

                    </sf:GridTemplateColumn.CellTemplate>

                </sf:GridTemplateColumn>

            </sf:SfDataGrid.Columns>

        </sf:SfDataGrid>

        <Button Click="AddTestObject_Click" Grid.Row="1" HorizontalAlignment="Left" Width="100" Margin="2">Add</Button>

    </Grid>

</Window>



And my Code Behind:-

using Syncfusion.UI.Xaml.Grid;

using System;

using System.Collections.Generic;

using System.Collections.ObjectModel;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

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;


namespace testgridcopy

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        private ViewModel mViewModel;

        public MainWindow()

        {

            InitializeComponent();

            mViewModel = new ViewModel();

            DataContext = mViewModel;

        }


        private void OnCurrentCellActivated(object sender, CurrentCellActivatedEventArgs e)

        {

            var datagrid = sender as SfDataGrid;

            if (datagrid != null)

            {

                if (datagrid.SelectionController != null)

                {

                    if (datagrid.SelectionController.CurrentCellManager != null)

                    {

                        if (datagrid.SelectionController.CurrentCellManager.CurrentCell != null)

                        {

                            //Here call the BeginEdit method to enter the edit mode in SfDataGrid

                            if (!datagrid.SelectionController.CurrentCellManager.CurrentCell.IsEditing)

                                datagrid.SelectionController.CurrentCellManager.BeginEdit();

                        }

                    }

                }

            }

        }


        private void AddTestObject_Click(object sender, RoutedEventArgs e)

        {

            mViewModel.TestObjects.Add(new TestObject());

        }


        private void DeleteTestObject_Click(object sender, EventArgs e)

        {

            mViewModel.TestObjects.Remove((sender as Button).Tag as TestObject);

        }

    }



    public class ViewModel

    {

        public ViewModel()

        { TestObjects = new ObservableCollection<TestObject>(); }


        public ObservableCollection<TestObject> TestObjects { get; set; }

    }


    public class TestObject

    {

        public string Property1 { get; set; }

        public string Property2 { get; set; }

        public string Property3 { get; set; }

    }

}




To replicate, follow these exact steps:-

  1. Click add to add a new row
  2. Click in the Property1 cell
  3. Type a value, e.g. "Hello"
  4. Select the value and press <ctrl-c> to put it on the clipboard
  5. Click the row header to select the entire row
  6. Press <ctrl-v> to paste the clipboard content
It will crash with a value cannot be null in ListDictionary.

The user pressing <ctrl-v> doesn't really make sense in this context because they're trying to paste a cell value when they have a whole row selected so the paste should be ignored.  How can I do that?

3 Replies 1 reply marked as answer

MA Manikanda Akash Munisamy Syncfusion Team August 13, 2024 02:10 PM UTC

Hi Declan Hillier,

We have reviewed the scenario where the application crashes when pasting a CellValue to the entire row. Based on the code you provided, we were able to reproduce the crash. To resolve this issue, we suggest declaring a MappingName for the GridTemplateColumn. This is important because, during clipboard operations, the value is committed to the cell using column.MappingName(corresponding PropertyDescriptor) in the order of the columns.


Please review the attached sample and let us know if you have any further concerns. If we have misunderstood your requirements, kindly provide additional details about the issue you are facing. This information will help us better understand the problem and provide a suitable solution.

Thank you for your cooperation

Regards,
Manikanda Akash M


Attachment: SfDataGrid_Demo4_8_172232a.zip

Marked as answer

DH Declan Hillier August 14, 2024 08:14 AM UTC

That worked perfectly.  Thank you for such a prompt response.



MA Manikanda Akash Munisamy Syncfusion Team August 15, 2024 03:38 AM UTC

Hi Declan Hillier,

You're welcome. Please get back to us if you need any further assistance, we will be happy to help you.

Regards,

Manikanda Akash M


Loader.
Up arrow icon