Skip to content

Commit fc33a98

Browse files
author
KB Bot
committed
Added new kb article gridview-prevent-focus
1 parent 88f32a0 commit fc33a98

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: Preventing Focus on RadGridView in WinForms
3+
description: Learn how to use a custom approach to prevent focus on RadGridView in WinForms.
4+
type: how-to
5+
page_title: How to Stop RadGridView from Receiving Focus in WinForms
6+
slug: gridview-prevent-focus
7+
tags: gridview, winforms, focus, focusable-property, custom-implementation
8+
res_type: kb
9+
ticketid: 745199
10+
---
11+
12+
## Environment
13+
|Product Version|Product|Author|
14+
|----|----|----|
15+
|2025.1.211|RadGridView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)|
16+
17+
## Description
18+
19+
There could be a requirement to prevent the RadGridView control from receiving focus even when interacting with it via mouse clicks or the Tab key. Setting the Focusable property to false does not fully stop the focus from being set on the grid.
20+
21+
## Solution
22+
23+
To prevent RadGridView from receiving focus, override its focus-related methods and use a custom grid behavior. Follow the steps below:
24+
25+
````C#
26+
public class MyGridView : RadGridView
27+
{
28+
protected override void OnGotFocus(EventArgs e)
29+
{
30+
// Prevent focus from being set
31+
// base.OnGotFocus(e);
32+
}
33+
34+
public override string ThemeClassName
35+
{
36+
get
37+
{
38+
return typeof(RadGridView).FullName;
39+
}
40+
}
41+
42+
protected override bool ProcessFocusRequested(RadElement element)
43+
{
44+
return false; // Prevent focus requests
45+
}
46+
}
47+
48+
public class MyGridBehavior : BaseGridBehavior
49+
{
50+
public override bool ProcessKey(KeyEventArgs keys)
51+
{
52+
if (keys.KeyCode == Keys.Tab)
53+
{
54+
return true; // Prevent focus change via Tab key
55+
}
56+
57+
return base.ProcessKey(keys);
58+
}
59+
60+
protected override bool OnMouseDownLeft(MouseEventArgs e)
61+
{
62+
return true; // Prevent focus change via mouse click
63+
}
64+
}
65+
66+
67+
// Usage:
68+
this.radGridView1 = new MyGridView();
69+
this.radGridView1.GridBehavior = new MyGridBehavior();
70+
71+
````
72+
73+
By implementing this code, RadGridView will no longer receive focus through mouse clicks or the Tab key. This approach ensures complete control over the focus behavior of the grid.
74+
75+
## See Also
76+
- [RadGridView Documentation](https://docs.telerik.com/devtools/winforms/controls/gridview/overview)

0 commit comments

Comments
 (0)