Copy the value of the calculated summary to the clipboard

Hello,

I'm using a SfDataGrid control in a WPF application. I have the summary row enabled on the columns that contains amounts in €.

I would like to add a feature where clicking on the summary copies the summary value to the clipboard but I have found no way to do this.

Is it possible?


Thank you,

Alessandro


4 Replies

DD Dhivyabharathi Dakshinamurthy Syncfusion Team March 6, 2024 06:44 PM UTC

Hi Alessandro,


Your requirement is to copy the summary value to the clipboard when clicking on the summary. We don't have direct support for copying summary values because SfDataGrid doesn't provide selection support for summaries. However, this functionality can be achieved by copying the summary using a ContextMenu. We have prepared the sample and attached with video demonstration for your reference.

Kindly refer the below code snippets.

<syncfusion:SfDataGrid.TableSummaryContextMenu>

     <ContextMenu>

         <MenuItem Header="Copy"

                   Command="{Binding CopyCommand,Source={StaticResource employeeInfoViewModel}}"

                   CommandParameter="{Binding}" >

         </MenuItem>

 

     </ContextMenu>

 </syncfusion:SfDataGrid.TableSummaryContextMenu>


Initialize the command in ViewModel Constructor.

public EmployeeInfoViewModel()

 {

     _orders = new ObservableCollection<OrderInfo>();

     this.GenerateOrders();

     CopyCommand = new BaseCommand(OnCopySummaryCountClicked);

 }


public void OnCopySummaryCountClicked(object obj)

 {

     var record = (obj as GridRecordContextMenuInfo).Record as SummaryRecordEntry;

     var data = new DataObject();

     var grid = (obj as GridRecordContextMenuInfo).DataGrid;

 

     string recordData = "";

 

     if (record.SummaryRow.ShowSummaryInRow)

         recordData = SummaryCreator.GetSummaryDisplayTextForRow(record, grid.View);

     else

     {

         foreach (var column in record.SummaryRow.SummaryColumns)

         {

             recordData += SummaryCreator.GetSummaryDisplayText(record, column.MappingName, grid.View) + "\t";

         }

     }

     data.SetText(recordData.ToString());

     Clipboard.SetDataObject(data);

 }


We hope this helps to achieve your requirement. Please let us know, if need any further assistance.



Attachment: SfDataGrid_CopySummary_337925f4.zip


AS Alessandro Spisso March 12, 2024 01:49 PM UTC

Hello,

Would it be possible to copy the value of the summary only on the summary text that I've right clicked? I don't need all the summary texts from the datagrid.


Thank you



DD Dhivyabharathi Dakshinamurthy Syncfusion Team March 13, 2024 05:40 PM UTC

Hi Alessandro,

 

We are currently analyzing your reported scenario. We need time to validate the scenario, we will provide an update on or before March 15, 2024.



DD Dhivyabharathi Dakshinamurthy Syncfusion Team March 15, 2024 03:51 PM UTC

Hi Alessandro,

Your requirement was to copy only the summary values without any text in the summary. This can be achieved by passing the column mapping name to the GetSummaryDisplayText method. This method returns only the summary values.

We have modified the code snippets. Please look at this.



public void OnCopySummaryCountClicked(object obj)

 {

     var record = (obj as GridRecordContextMenuInfo).Record as SummaryRecordEntry;

     var data = new DataObject();

     var grid = (obj as GridRecordContextMenuInfo).DataGrid;

 

     string recordData = "";

 

     if (record.SummaryRow.ShowSummaryInRow)

     {

         foreach (var column in record.SummaryRow.SummaryColumns)

         {

             recordData += SummaryCreator.GetSummaryDisplayText(record, column.MappingName, grid.View);

         }

     }

     data.SetText(recordData.ToString());

     Clipboard.SetDataObject(data);

 }



Loader.
Up arrow icon