How to exclude header while copying in WPF GridControl when table is selected?
All the cells in WPF GridControl can be selected by click the top left header cell like Excel. If user copies by pressing Ctrl+C when table is selected, then the header cells also will get copied. You can disable the copy of the header cells to clipboard in the following two ways.
Way 1: Exclude header cells during copy operation
You can select whole table when clicking the top left header cell. If you want to avoid the header cells while copying the whole table and pasting it to excel file, you can remove the selection of header cell (first row and column) from the SelectedRanges collection by using the RemoveAt() method and add the cell range from (1, 1) to SelectedRanges in the ClipboardCopy event.
Refer the below code for your reference.
C#
grid.Model.ClipboardCopy += Model_ClipboardCopy;
private void Model_ClipboardCopy(object sender, Syncfusion.Windows.Controls.Grid.GridCutPasteEventArgs e)
{
if (grid.Model.SelectedRanges.Count > 0 && grid.Model.SelectedRanges[0].IsTable)
{
grid.Model.SelectedRanges.RemoveAt(0);
grid.Model.SelectedRanges.Add(GridRangeInfo.Cells(1, 1, grid.Model.RowCount - 1, grid.Model.ColumnCount - 1));
}
}
Way 2: Avoid selection for header cells when clicking top left cell
In another way, The selection can be controlled by handling SelectionChanging event. If the table is selected, then you can change the range by excluding headers by setting GridSelectionChangingEventArgs.Range property.
Refer the below code for your reference.
grid.Model.SelectionChanging += Model_SelectionChanging;
private void Model_SelectionChanging(object sender, GridSelectionChangingEventArgs e)
{
if (e.Range.IsTable)
{
e.Range = GridRangeInfo.Cells(1, 1, grid.Model.RowCount, grid.Model.ColumnCount);
}
}
Sample: View sample in GitHub