|
| 1 | +--- |
| 2 | +title: Forcing RadGridView to Snap One Row During Scrolling |
| 3 | +description: Learn how to force RadGridView in UI for WinForms to snap one row per scrolling, including mouse wheel scrolling. |
| 4 | +type: how-to |
| 5 | +page_title: Ensure RadGridView Scroll Rows One-by-One When Scrolling |
| 6 | +meta_title: Ensure RadGridView Scroll Rows One-by-One When Scrolling |
| 7 | +slug: gridview-snap-one-row-per-scroll |
| 8 | +tags: gridview, ui for winforms, scrolling, mousewheel, scrollmode, row-scrolling |
| 9 | +res_type: kb |
| 10 | +ticketid: 1702130 |
| 11 | +--- |
| 12 | + |
| 13 | +|Product Version|Product|Author| |
| 14 | +|----|----|----| |
| 15 | +|2025.3.812|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)| |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +In this tutorial, we will demonstrate how to snap to row gridlines and display rows one by one during scrolling, especially when using the mouse wheel. |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +To achieve row-by-row scrolling in RadGridView, use discrete scrolling mode and add custom handling for mouse wheel scrolling. |
| 24 | + |
| 25 | +1. Set the scrolling mode to discrete to enable row-by-row scrolling when using the scrollbar. This is further described in the [Scroll Modes](https://docs.telerik.com/devtools/winforms/controls/gridview/features/scrolling/scroll-modes) help article. |
| 26 | + |
| 27 | +````C# |
| 28 | + |
| 29 | + this.radGridView1.TableElement.RowScroller.ScrollMode = ItemScrollerScrollModes.Discrete; |
| 30 | + |
| 31 | +```` |
| 32 | + |
| 33 | +2. To apply the same behavior for mouse wheel scrolling, handle the `MouseWheel` event of RadGridView. |
| 34 | + |
| 35 | +````C# |
| 36 | +this.radGridView1.MouseWheel += RadGridView1_MouseWheel; |
| 37 | + |
| 38 | +```` |
| 39 | + |
| 40 | +3. Implement the `RadGridView1_MouseWheel` event to calculate new scroll positions based on row height and wheel direction. |
| 41 | + |
| 42 | +````C# |
| 43 | + |
| 44 | +private void RadGridView1_MouseWheel(object sender, MouseEventArgs e) |
| 45 | +{ |
| 46 | + var grid = sender as RadGridView; |
| 47 | + if (grid == null || grid.RowCount == 0) return; |
| 48 | + |
| 49 | + // Positive delta = scroll up |
| 50 | + int direction = e.Delta > 0 ? -1 : 1; |
| 51 | + |
| 52 | + var vScrollBar = grid.TableElement.VScrollBar; |
| 53 | + |
| 54 | + // Scroll by one row at a time |
| 55 | + int newValue = vScrollBar.Value + (direction * grid.TableElement.RowHeight); |
| 56 | + newValue = Math.Max(vScrollBar.Minimum, Math.Min(vScrollBar.Maximum, newValue)); |
| 57 | + |
| 58 | + if (direction == 1 && this.radGridView1.TableElement.VisualRows.Last().RowInfo == this.radGridView1.Rows.Last()) |
| 59 | + { |
| 60 | + // hit last row |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + vScrollBar.Value = newValue; |
| 65 | + } |
| 66 | + |
| 67 | + // Prevent default scrolling |
| 68 | + ((HandledMouseEventArgs)e).Handled = true; |
| 69 | +} |
| 70 | + |
| 71 | +```` |
| 72 | + |
| 73 | +This configuration ensures RadGridView scrolls one row at a time, maintaining alignment with gridlines for both scrollbar and mouse wheel interactions. |
| 74 | + |
| 75 | +## See Also |
| 76 | + |
| 77 | +* [Scroll Modes Documentation](https://docs.telerik.com/devtools/winforms/controls/gridview/features/scrolling/scroll-modes) |
| 78 | +* [RadGridView Overview](https://docs.telerik.com/devtools/winforms/controls/gridview/overview) |
0 commit comments