Records within a View (rows in a Grid View and cards in a Card View) are identified by their handles. The FocusedRowHandle property allows to get the currently selected row's handle, or to move focus to a row with the specific handle. In latter case, the Data Grid expands a group that contains this row, and scrolls the View up or down to make this row visible.
If there is no focused record, the FocusedRowHandle property returns the GridControl.InvalidRowHandle value. For instance, this will be returned when the View is empty. Note that if a Grid View has any records, you cannot set the FocusedRowHandle property to the GridControl.InvalidRowHandle value to de-select a currently focused row.
Note
The FocusedRowHandle method works only when a Grid is completely initialized. If you need to call it when the form loads, call the GridControl.ForceInitialize method first to ensure the control is ready.
Do not use the FocusedRowHandle property to determine whether the Grid control is focused: the Grid returns valid FocusedRowHandle values even when the control itself is not focused.
When row focus changes, the FocusedRowChanged and FocusedRowObjectChanged events are raised.
In Layout (see LayoutView)s and Card (see CardView) Views, when you change the FocusedRowHandle property value the FocusedColumn property resets to null. In this instance, you may want to manually edit the FocusedColumn property to move focus to a specific card field.

Multiple Row\Card Selection Mode
The focused row\card is the row (card) that accepts input from an end-user. For that reason, a View can have only one focused row even if multiple row (card) selection is on.
In multiple row\card selection mode, a focused row can be unselected (not highlighted) and vice versa, a selected (highlighted) row can be unfocused. The following images illustrate these cases.

|
The focused "Condiments" row is selected. The GetSelectedRows method returns three row handles, including the focused row handle.
|

|
The focused "Condiments" row is not selected, selected "Dairy Products" and "Grains, Cereals" rows are not focused. The GetSelectedRows method returns an array that contains two handles and does not include the focused row handle.
|
See the Multiple Row and Cell Selection topic for more information.
Note
Detail pattern Views do not contain data and they are never displayed within XtraGrid. So, the FocusedRowHandle member must not be invoked for these Views.
The FocusedRowHandle member can only be used with real Views that are displayed within the Grid Control. The real Views with which an end-user interacts at runtime can be accessed using the following methods.

Example
The following example creates a GridControl (see GridControl) at runtime and shows how to perform basic customization tasks:
- Bind the grid to a data source
- Access the View that presents the underlying data
- Access columns
- Assign an in-place editor (a combobox) to a column
- Sort and group data
- Calculate total and group summaries
- Create a filter
- Hide columns and calculate column "best" widths
- Expand group rows
- Focus a specific cell
- Specify DataAnnotation attributes (column display names and data formats) at the data source level

C# |
using DevExpress.Data;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
GridControl gridControl1 = new GridControl();
gridControl1.Parent = this;
gridControl1.Dock = DockStyle.Fill;
gridControl1.DataSource = DataHelper.GetData(30);
GridView gridView1 = gridControl1.MainView as GridView;
GridColumn colCompany = gridView1.Columns["CompanyName"];
GridColumn colID = gridView1.Columns["ID"];
GridColumn colDate = gridView1.Columns["RequiredDate"];
GridColumn colPayment = gridView1.Columns["Value"];
GridColumn colProcessed = gridView1.Columns["Processed"];
RepositoryItemComboBox riComboBox = new RepositoryItemComboBox();
riComboBox.Items.AddRange(DataHelper.companies);
gridControl1.RepositoryItems.Add(riComboBox);
colCompany.ColumnEdit = riComboBox;
colID.Visible = false;
colCompany.GroupIndex = 0;
colDate.SortIndex = 0;
colDate.SortOrder = DevExpress.Data.ColumnSortOrder.Descending;
gridView1.OptionsView.ShowGroupedColumns = true;
gridView1.ExpandAllGroups();
gridView1.ActiveFilterString = "[RequiredDate]>= #" + DateTime.Today.ToString() + "#";
colDate.Summary.Add(SummaryItemType.Count, colDate.FieldName, "Count={0}");
colDate.Summary.Add(SummaryItemType.Max, colDate.FieldName, "Max={0:d}");
gridView1.OptionsView.ShowFooter = true;
GridGroupSummaryItem item = new GridGroupSummaryItem();
item.FieldName = colCompany.FieldName;
item.SummaryType = DevExpress.Data.SummaryItemType.Count;
gridView1.GroupSummary.Add(item);
GridGroupSummaryItem item1 = new GridGroupSummaryItem();
item1.FieldName = colPayment.FieldName;
item1.SummaryType = SummaryItemType.Sum;
item1.DisplayFormat = "group total={0:c2}";
item1.ShowInGroupColumnFooter = colPayment;
gridView1.GroupSummary.Add(item1);
gridView1.OptionsBehavior.AlignGroupSummaryInGroupRow = DevExpress.Utils.DefaultBoolean.True;
gridView1.FocusedRowHandle = 1;
gridView1.FocusedColumn = colCompany;
colDate.BestFit();
colProcessed.BestFit();
}
}
public class Record : INotifyPropertyChanged {
public Record() {
}
int id;
public int ID {
get { return id; }
set {
if (id != value) {
id = value;
OnPropertyChanged();
}
}
}
string text;
[DisplayName("Company")]
public string CompanyName {
get { return text; }
set {
if (text != value) {
if (string.IsNullOrEmpty(value))
throw new Exception();
text = value;
OnPropertyChanged();
}
}
}
Nullable<decimal> val;
[DataType(DataType.Currency)]
[DisplayName("Payment")]
public Nullable<decimal> Value {
get { return val; }
set {
if (val != value) {
val = value;
OnPropertyChanged();
}
}
}
DateTime dt;
[DisplayFormat(DataFormatString = "d")]
public DateTime RequiredDate {
get { return dt; }
set {
if (dt != value) {
dt = value;
OnPropertyChanged();
}
}
}
bool state;
public bool Processed {
get { return state; }
set {
if (state != value) {
state = value;
OnPropertyChanged();
}
}
}
public override string ToString() {
return string.Format("ID = {0}, Text = {1}", ID, CompanyName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class DataHelper {
public static string[] companies = new string[] { "Hanari Carnes", "Que Delícia", "Romero y tomillo", "Mère Paillarde",
"Comércio Mineiro", "Reggiani Caseifici", "Maison Dewey" };
public static BindingList<Record> GetData(int count) {
BindingList<Record> records = new BindingList<Record>();
Random rnd = new Random();
for (int i = 0; i < count; i++) {
int n = rnd.Next(10);
records.Add(new Record() {
ID = i + 100,
CompanyName = companies[i % companies.Length],
RequiredDate = DateTime.Today.AddDays(n - 5),
Value = i % 2 == 0 ? (i + 1) * 123 : i * 231,
Processed = i % 2 == 0,
});
};
return records;
}
}
}
|
VB |
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Columns
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim gridControl1 As GridControl = New GridControl()
gridControl1.Parent = Me
gridControl1.Dock = DockStyle.Fill
gridControl1.DataSource = DataHelper.GetData(30)
Dim gridView1 As GridView = TryCast(gridControl1.MainView, GridView)
Dim colCompany As GridColumn = gridView1.Columns("CompanyName")
Dim colID As GridColumn = gridView1.Columns("ID")
Dim colDate As GridColumn = gridView1.Columns("RequiredDate")
Dim colPayment As GridColumn = gridView1.Columns("Value")
Dim colProcessed As GridColumn = gridView1.Columns("Processed")
Dim riComboBox As RepositoryItemComboBox = New RepositoryItemComboBox()
riComboBox.Items.AddRange(DataHelper.companies)
gridControl1.RepositoryItems.Add(riComboBox)
colCompany.ColumnEdit = riComboBox
colID.Visible = False
colCompany.GroupIndex = 0
colDate.SortIndex = 0
colDate.SortOrder = DevExpress.Data.ColumnSortOrder.Descending
gridView1.OptionsView.ShowGroupedColumns = True
gridView1.ExpandAllGroups()
gridView1.ActiveFilterString = "[RequiredDate]>= #" & DateTime.Today.ToString() & "#"
colDate.Summary.Add(SummaryItemType.Count, colDate.FieldName, "Count={0}")
colDate.Summary.Add(SummaryItemType.Max, colDate.FieldName, "Max={0:d}")
gridView1.OptionsView.ShowFooter = True
Dim item As GridGroupSummaryItem = New GridGroupSummaryItem()
item.FieldName = colCompany.FieldName
item.SummaryType = DevExpress.Data.SummaryItemType.Count
gridView1.GroupSummary.Add(item)
Dim item1 As GridGroupSummaryItem = New GridGroupSummaryItem()
item1.FieldName = colPayment.FieldName
item1.SummaryType = SummaryItemType.Sum
item1.DisplayFormat = "group total={0:c2}"
item1.ShowInGroupColumnFooter = colPayment
gridView1.GroupSummary.Add(item1)
gridView1.OptionsBehavior.AlignGroupSummaryInGroupRow = DevExpress.Utils.DefaultBoolean.True
gridView1.FocusedRowHandle = 1
gridView1.FocusedColumn = colCompany
colDate.BestFit()
colProcessed.BestFit()
End Sub
End Class
Public Class Record
Implements INotifyPropertyChanged
Public Sub New()
End Sub
Private id_Renamed As Integer
Public Property ID() As Integer
Get
Return id_Renamed
End Get
Set(ByVal value As Integer)
If id_Renamed <> value Then
id_Renamed = value
OnPropertyChanged()
End If
End Set
End Property
Private text As String
<DisplayName("Company")>
Public Property CompanyName() As String
Get
Return text
End Get
Set(ByVal value As String)
If text <> value Then
If String.IsNullOrEmpty(value) Then
Throw New Exception()
End If
text = value
OnPropertyChanged()
End If
End Set
End Property
Private val? As Decimal
<DataType(DataType.Currency), DisplayName("Payment")>
Public Property Value() As Decimal?
Get
Return val
End Get
Set(ByVal value? As Decimal)
If Not val.Equals(value) Then
val = value
OnPropertyChanged()
End If
End Set
End Property
Private dt As Date
<DisplayFormat(DataFormatString:="d")>
Public Property RequiredDate() As Date
Get
Return dt
End Get
Set(ByVal value As Date)
If dt <> value Then
dt = value
OnPropertyChanged()
End If
End Set
End Property
Private state As Boolean
Public Property Processed() As Boolean
Get
Return state
End Get
Set(ByVal value As Boolean)
If state <> value Then
state = value
OnPropertyChanged()
End If
End Set
End Property
Public Overrides Function ToString() As String
Return String.Format("ID = {0}, Text = {1}", ID, CompanyName)
End Function
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Class DataHelper
Public Shared companies() As String = {"Hanari Carnes", "Que Delícia", "Romero y tomillo", "Mère Paillarde", "Comércio Mineiro", "Reggiani Caseifici", "Maison Dewey"}
Public Shared Function GetData(ByVal count As Integer) As BindingList(Of Record)
Dim records As New BindingList(Of Record)()
Dim rnd As New Random()
For i As Integer = 0 To count - 1
Dim n As Integer = rnd.Next(10)
records.Add(New Record() With {
.ID = i + 100,
.CompanyName = companies(i Mod companies.Length),
.RequiredDate = Date.Today.AddDays(n - 5),
.Value = If(i Mod 2 = 0, (i + 1) * 123, i * 231),
.Processed = i Mod 2 = 0
})
Next i
Return records
End Function
End Class
|