|
| 1 | +--- |
| 2 | +title: Preventing Column Dragging Over Pinned Columns in RadGridView for WinForms |
| 3 | +description: Learn how to restrict the reordering of RadGridView columns in WinForms by preventing columns from being dragged over pinned columns. |
| 4 | +type: how-to |
| 5 | +page_title: How to Restrict Column Dragging Over Pinned Columns in RadGridView for WinForms |
| 6 | +slug: prevent-column-drag-over-pinned-radgridview-winforms |
| 7 | +tags: radgridview, winforms, drag-and-drop, pinned-columns, radgriddragdropservice |
| 8 | +res_type: kb |
| 9 | +ticketid: 1667283 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadGridView for WinForms</td> |
| 19 | +</tr> |
| 20 | +</tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +When working with pinned columns in RadGridView, I want to prevent the user from dragging unpinned columns over or between the pinned columns. This issue arises when the first two columns are pinned, but an unpinned column can still be moved to the leftmost position or between pinned columns, effectively increasing the number of pinned columns. |
| 26 | + |
| 27 | +This KB article also answers the following questions: |
| 28 | +- How can I restrict column reordering in RadGridView with pinned columns? |
| 29 | +- Is it possible to disable dragging columns over pinned columns in RadGridView for WinForms? |
| 30 | +- What method can prevent a column from being moved over pinned columns in RadGridView? |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To achieve the desired behavior, leverage the [RadDragDropService](https://docs.telerik.com/devtools/winforms/controls/gridview/drag-and-drop/radgridviewdragdropservice) of RadGridView, which handles the drag-and-drop operations. By handling the `PreviewDragOver` event, you can prevent a column from being dropped over a pinned column. |
| 35 | + |
| 36 | +Follow these steps to implement the solution: |
| 37 | + |
| 38 | +1. Access the `RadDragDropService` from your `RadGridView` instance. |
| 39 | +2. Subscribe to the `PreviewDragOver` event of the `RadDragDropService`. |
| 40 | +3. In the event handler, check if the target of the drag operation is a pinned column. If so, set `e.CanDrop` to `false` to prevent the drop. |
| 41 | + |
| 42 | +Here is a code snippet illustrating the process: |
| 43 | + |
| 44 | +```csharp |
| 45 | +// Access the RadDragDropService |
| 46 | +RadDragDropService svc = this.radGridView1.GridViewElement.GetService<RadDragDropService>(); |
| 47 | + |
| 48 | +// Subscribe to the PreviewDragOver event |
| 49 | +svc.PreviewDragOver += svc_PreviewDragOver; |
| 50 | + |
| 51 | +// Event handler to prevent dropping over pinned columns |
| 52 | +private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e) |
| 53 | +{ |
| 54 | + if (e.HitTarget is GridHeaderCellElement) |
| 55 | + { |
| 56 | + GridHeaderCellElement headerCell = e.HitTarget as GridHeaderCellElement; |
| 57 | + if (headerCell.ColumnInfo.IsPinned) |
| 58 | + { |
| 59 | + // Prevent dropping over pinned columns |
| 60 | + e.CanDrop = false; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +By implementing this solution, you ensure that users cannot drag unpinned columns over or between pinned columns, maintaining the integrity of your column layout in RadGridView. |
| 67 | + |
| 68 | +## See Also |
| 69 | + |
| 70 | +- [RadGridView for WinForms Documentation - Drag and Drop](https://docs.telerik.com/devtools/winforms/controls/gridview/drag-and-drop/radgridviewdragdropservice) |
| 71 | +- [RadGridView for WinForms Documentation - Pinned Columns](https://docs.telerik.com/devtools/winforms/controls/gridview/columns/pinned-columns) |
0 commit comments