Articles in this section
Category / Section

How to paste copied rows to the empty UWP DataGrid (SfDataGrid)?

1 min read

By default, SfDataGrid does not allows you to paste the records when grid is empty. You can achieve this by overriding OnAttached method in SfDataGrid class.

public class SfDataGridBehavior : Behavior<SfDataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.GridCopyPaste = new CustomCopyPaste(this.AssociatedObject);
        this.AssociatedObject.SelectionController = new CustomGridSelectionController(this.AssociatedObject);
        this.AssociatedObject.Loaded += AssociatedObject_Loaded;
    }
 
    private void AssociatedObject_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        this.AssociatedObject.GetVisualContainer().PointerReleased += SfDataGridBehavior_PointerReleased;
    }
 
    private async void SfDataGridBehavior_PointerReleased(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () =>
        {
            if (this.AssociatedObject.View != null && this.AssociatedObject.View.Records.Count == 0)
            {
                this.AssociatedObject.Focus(Windows.UI.Xaml.FocusState.Programmatic);
            }
        });
    }
}

 

Demo to show the records paste from notepad

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied