Articles in this section
Category / Section

How to copy all the rows in GridDataControl using ContextMenu?

2 mins read

In GridDataControl, you can copy all the data in the grid by using the ContextMenu. You can use customize ContextMenu by setting EnableContextMenu as True and ContextMenuOptions as Custom in GridDataControl. Also, you have to define the DelegateCommand for CopyAll MenuItem in ViewModel and bind that DelegateCommand to CopyAll MenuItem in RecordContextMenuItems.

The following code example demonstrates how to define the RecordContextMenuItems in GridDataControl.

XAML

<syncfusion:GridDataControl ItemsSource="{Binding GDCSource}" 
                                x:Name="SyncGrid"
                                AllowSelection="Row" 
                                EnableContextMenu="True"
                                CopyPasteOption="CopyText" 
                                ContextMenuOptions="Custom"      
                                AutoPopulateColumns="True">
    <syncfusion:GridDataControl.RecordContextMenuItems>
        <local:RecordMenuItems>
            <MenuItem Command="{Binding Cut}" CommandParameter="{x:Reference SyncGrid}" Header="Cut" />
            <MenuItem Command="{Binding Copy}" CommandParameter="{x:Reference SyncGrid}" Header="Copy" />
            <MenuItem Command="{Binding CopyAll}" CommandParameter="{x:Reference SyncGrid}" Header="CopyAll" />
            <MenuItem Command="{Binding Paste}" CommandParameter="{x:Reference SyncGrid}" Header="Paste" />
        </local:RecordMenuItems>
    </syncfusion:GridDataControl.RecordContextMenuItems>
</syncfusion:GridDataControl>

You can copy all the data in grid by using grid.Model.CutPaste.CopyRange method by passing GridRangeInfo of GridDataControl by using the DelegateCommand.

The following code example demonstrates how you can copy all the data in GridDataControl by using the DelegateCommand.

C#

public class PersonModel
{
    public PersonModel()
    {
        this.GDCSource = Getsource();
        this._copyall = new DelegateCommand<object>(CopyAllOperation);
    }
 
    private ObservableCollection<Person> _gdcsource;
    public ObservableCollection<Person> GDCSource
    {
        get { return _gdcsource; }
        set { _gdcsource = value; }
    }
    //Defining the Delegate Command for CopyAll
    private DelegateCommand<object> _copyall;
 
    public DelegateCommand<object> CopyAll
    {
        get { return _copyall; }
    }
    //Preforming CopyAll the data in the Grid
    void CopyAllOperation(object param)
    {
        var grid = (param as GridDataControl);
        int rowcount = grid.Model.View.Records.Count;
        int columncount = grid.Model.ColumnCount;
        GridRangeInfo range = GridRangeInfo.Cells(0, 0, rowcount, columncount);
        GridRangeInfoList rangelist = new GridRangeInfoList();
        rangelist.Add(range);
        grid.Model.CutPaste.CopyRange(rangelist, false, true);
    }
}

 

You can refer to the following sample link to copy all the rows in WPF.

 

Sample Link:  CopyAllRows_GDC

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