Example E460
Visible to All Users

WinForms Data Grid - Prevent the group row from being focused

This example handles the FocusedRowChanged event to prevent a user from focusing a group row. The user can focus only a data row.

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid; using System; using System.ComponentModel; using System.Windows.Forms; namespace Q183147 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Random rand = new Random(); var list = new BindingList<Item>(); for (int i = 0; i < 50; i++) list.Add(new Item() { ID = i, Name = "Name" + i, Category = rand.Next(0, 5) }); gridControl1.DataSource = list; } private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { int focusedRowHandle = -1; if (e.FocusedRowHandle == GridControl.NewItemRowHandle || e.FocusedRowHandle == GridControl.AutoFilterRowHandle) return; GridView view = (GridView)sender; if (e.FocusedRowHandle < 0) { if (e.PrevFocusedRowHandle == GridControl.InvalidRowHandle) focusedRowHandle = 0; else if (Control.MouseButtons == MouseButtons.Left || Control.MouseButtons == MouseButtons.Right) focusedRowHandle = e.PrevFocusedRowHandle; else { int prevRow = view.GetVisibleIndex(e.PrevFocusedRowHandle); int currRow = view.GetVisibleIndex(e.FocusedRowHandle); if (prevRow > currRow) focusedRowHandle = e.PrevFocusedRowHandle - 1; else focusedRowHandle = e.PrevFocusedRowHandle + 1; if (focusedRowHandle < 0) focusedRowHandle = 0; if (focusedRowHandle >= view.DataRowCount) focusedRowHandle = view.DataRowCount - 1; } view.FocusedRowHandle = focusedRowHandle < 0 ? 0 : focusedRowHandle; } } public class Item { public int ID { get; set; } public string Name { get; set; } public int Category { get; set; } } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.