Skip to content

Commit

Permalink
Merge pull request #644 from telerik/new-kb-prevent-column-drag-over-…
Browse files Browse the repository at this point in the history
…pinned-radgridview-winforms-e15c72f0216146808edb804998f45c41

Added new kb article prevent-column-drag-over-pinned-radgridview-winforms
  • Loading branch information
DinkoK authored Oct 24, 2024
2 parents 42d89a7 + dd737e2 commit bf21e87
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Prevent Column Dragging Over Pinned Columns in RadGridView for WinForms
description: Learn how to restrict the reordering of RadGridView columns by preventing columns from being dragged over pinned columns.
type: how-to
page_title: How to Restrict Column Dragging Over Pinned Columns in RadGridView for WinForms
slug: gridview-prevent-column-drag-over-pinned
tags: gridview, winforms, drag-and-drop, pinned-columns, radgriddragdropservice
res_type: kb
ticketid: 1667283
---

## Environment

|Product Version|Product|Author|
|----|----|----|
|2024.3.924|RadGridView for WinForms|[Nadya Todorova](https://www.telerik.com/blogs/author/nadya-karaivanova)|

## Description

When working with pinned columns in RadGridView, the client may want to prevent dragging unpinned columns over or between the pinned columns. Currently, unpinned column can be moved and placed between pinned columns, effectively increasing the number of pinned columns.

## Solution

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.

Follow these steps to implement the solution:

````C#
// Access the RadDragDropService
RadDragDropService svc = this.radGridView1.GridViewElement.GetService<RadDragDropService>();

// Subscribe to the PreviewDragOver event
svc.PreviewDragOver += svc_PreviewDragOver;

// Event handler to prevent dropping over pinned columns
private void svc_PreviewDragOver(object sender, RadDragOverEventArgs e)
{
if (e.HitTarget is GridHeaderCellElement)
{
GridHeaderCellElement headerCell = e.HitTarget as GridHeaderCellElement;
if (headerCell.ColumnInfo.IsPinned)
{
// Prevent dropping over pinned columns
e.CanDrop = false;
}
}
}

````

## See Also

- [RadGridView for WinForms Documentation - Drag and Drop](https://docs.telerik.com/devtools/winforms/controls/gridview/drag-and-drop/radgridviewdragdropservice)
- [RadGridView for WinForms Documentation - Pinned Columns](https://docs.telerik.com/devtools/winforms/controls/gridview/columns/pinned-columns)

0 comments on commit bf21e87

Please sign in to comment.