Articles in this section
Category / Section

How to copy a cell value instead of formula in WinForms Spreadsheet (SfSpreadsheet)?

2 mins read

Copy a cell value instead of formula cell

WinForms Spreadsheet provides support to copy the values in a formula cell instead of formula by overriding the Copy method of SpreadsheetCopyPaste Class.

In the sample, create a CustomCopyPaste class inherited from SpreadsheetCopyPaste class and override the Copy method like below code example.

C#

public class CustomCopyPaste : SpreadsheetCopyPaste
{
  public Spreadsheet spreadSheet
  {
    get;
    set;
  }
  public CustomCopyPaste(Spreadsheet spread)
            : base()
  {
     spreadSheet = spread;
  }
  public override void Copy()
  {
    var copyRange = spreadSheet.ActiveGrid.SelectedRanges.ActiveRange;
    var excelSourceRange = copyRange.ConvertGridRangeToExcelRange(spreadSheet.ActiveGrid);
    SourceWorkbookRange = spreadSheet.ActiveGrid.Worksheet.Range[excelSourceRange];
    CopyClipboard(copyRange);
    SourceGrid = spreadSheet.ActiveGrid;
  }
 
   public void CopyClipboard(GridRangeInfo copyRange)
   {
      var sb = new StringBuilder();
      for (int i = copyRange.Top; i <= copyRange.Bottom; i++)
      {
          for (int j = copyRange.Left; j <= copyRange.Right; j++)
          {
              sb.Append(SourceWorkbookRange[i, j].DisplayText);
              if (j != copyRange.Right)
                 sb.Append("\t");
          }
          if (i != copyRange.Bottom)
            sb.Append("\r\n");
      }
       var data = new DataObject();
       if (sb.Length == 0)
       {
          sb.Append(" ");
       }
       data.SetText(sb.ToString());
       Clipboard.SetDataObject(data);
       sb.Clear();
   }
}

In the above code example, the copied range’s DisplayText is copied to the Clipboard instead of Value.

Sample: WF 

Conclusion

I hope you enjoyed learning about how to copy a cell value instead of formula in WinForms Spreadsheet (SfSpreadsheet).

You can refer to our WinForms Spreadsheet’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms Spreadsheet documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms Spreadsheet and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

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