.NET MAUI DataGrid Current Cell
DataGrid control gives you the option to define the current cell using the CurrentCell
(of type DataGridCellInfo
) property. You can modify the current cell programmatically, during a keyboard navigation, using a mouse, etc.
Event
-
CurrentCellChanged
event is invoked when the current cell changes during a keyboard navigation. TheCurrentCellChanged
event handler receives two parameters:- The sender argument, which is of type object, but can be cast to the
RadDataGrid
type. - A
CurrentCellChangedEventArgs
object, which provides the following properties:-
OldCurrentCell
—Gets the previouslyCurrentCell
. -
NewCurrentCell
—Gets the newCurrentCell
.
-
- The sender argument, which is of type object, but can be cast to the
Styling
Easily apply a style to the current cell using the CurrentCellStyle
(of type DataGridBorderStyle
). Apply BackgroundColor
, BorderColor
and BorderThickness
.
Example
Here is an example with the CurrentCell
, CurrentCellChanged
and CurrentCellStyle
.
1 The used ViewModel and Business object:
ViewModel:
public class ViewModel : NotifyPropertyChangedBase
{
private DataGridCellInfo cell;
public ViewModel()
{
this.People = new ObservableCollection<Person>()
{
new Person { Name = "Kiko", Age = 23, Department = "Production" },
new Person { Name = "Jerry", Age = 23, Department = "Accounting and Finance"},
new Person { Name = "Ethan", Age = 51, Department = "Marketing" },
new Person { Name = "Isabella", Age = 25, Department = "Marketing" },
new Person { Name = "Joshua", Age = 45, Department = "Production" },
new Person { Name = "Logan", Age = 26, Department = "Production"},
new Person { Name = "Aaron", Age = 32, Department = "Production" },
new Person { Name = "Elena", Age = 37, Department = "Accounting and Finance"},
new Person { Name = "Ross", Age = 30, Department = "Marketing" },
};
}
public DataGridCellInfo Cell
{
get => this.cell;
set
{
if (this.cell != value)
{
this.cell = value;
this.OnPropertyChanged();
}
}
}
public ObservableCollection<Person> People { get; set; }
}
Business object:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
}
2 DataGrid definition in XAML
<telerik:RadDataGrid x:Name="dataGrid"
Grid.Row="1"
ItemsSource="{Binding People}"
CurrentCellChanged="dataGrid_CurrentCellChanged"
CurrentCell="{Binding Cell, Mode=TwoWay}"
CurrentCellStyle="{StaticResource CurrentCellStyle}"/>
3 The style used for the CurrentCellStyle
and defined in the page's resources:
<telerik:DataGridBorderStyle x:Key="CurrentCellStyle"
BorderColor="Black"
BackgroundColor="LightGray"
BorderThickness="2"/>
4 The CurrentCellChanged
event:
private void dataGrid_CurrentCellChanged(object sender, CurrentCellChangedEventArgs e)
{
var data = e.NewCurrentCell;
this.cellInfo.Text = data.Value.ToString();
}
This is the final result: