Articles in this section
Category / Section

How to paste the empty string while using cell selection in WinUI DataGrid (SfDataGrid)?

1 min read

In WinUI DataGrid (SfDataGrid), the default behavior does not allow an empty string to be pasted into a GridCell when the SelectionUnit is set to Cell or Any. However, this behavior can be modified by overriding the PasteToRow method in the DataGridClipboardController class.

public class CustomCopyPaste : DataGridClipboardController
{
   private SfDataGrid dataGrid;
   public CustomCopyPaste(SfDataGrid sfgrid)
       : base(sfgrid)
   {
       dataGrid = sfgrid;
   }

   protected override void PasteToRow(object clipboardcontent, object selectedRecords)
   {
       if (dataGrid.SelectionUnit == GridSelectionUnit.Row)
           base.PasteToRow(clipboardcontent, selectedRecords);
       else
       {
           //Get the copied value.
           clipboardcontent = Regex.Split(clipboardcontent.ToString(), @"\t");
           var copyValue = (string[])clipboardcontent;

           int cellcount = copyValue.Count();
           var selectionContoller = this.dataGrid.SelectionController as GridCellSelectionController;
           var lastselectedindex = selectionContoller.CurrentCellManager.CurrentRowColumnIndex.ColumnIndex;
           //Get the PressedRowColumnIndex value using reflection.
           var Propertyinfo = (this.dataGrid.SelectionController as GridCellSelectionController).GetType().GetProperty("PressedRowColumnIndex", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
           var pressedrowcolumnindex = Propertyinfo.GetValue(this.dataGrid.SelectionController);
           var pressedindex = ((RowColumnIndex)(pressedrowcolumnindex)).ColumnIndex;
           var pastecolumnindex = pressedindex < lastselectedindex ? pressedindex : lastselectedindex;

           int columnindex = 0;
           var columnstartindex = this.dataGrid.ResolveToGridVisibleColumnIndex(pastecolumnindex);
           for (int i = columnstartindex; i &lt; cellcount + columnstartindex; i++)
           {
               if (dataGrid.PasteOption.HasFlag(GridPasteOptions.IncludeHiddenColumn))
               {
                   if (dataGrid.Columns.Count &lt;= i)
                       break;
                   PasteToCell(selectedRecords, dataGrid.Columns[i], copyValue[columnindex]);
                   columnindex++;
               }
               else
               {
                   if (dataGrid.Columns.Count &lt;= i)
                       break;
                   //Paste the copied value here include empty string value.
                   if (!dataGrid.Columns[i].IsHidden)
                   {
                       PasteToCell(selectedRecords, dataGrid.Columns[i], copyValue[columnindex]);
                       columnindex++;
                   }
                   else
                       cellcount++;
               }
           }
       }
   }
}

Shows the empty string pasted in SfDataGrid

Take a moment to peruse the WinUI DataGrid - Clipboard Operations documentation, where you can find about the clipboard operations with code examples.

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