|
| 1 | +--- |
| 2 | +title: Selecting the First Row in DataGrid by Default Using MVVM in UI for .NET MAUI |
| 3 | +description: Learn how to select the first row in a UI for .NET MAUI DataGrid by default using the MVVM approach. |
| 4 | +type: how-to |
| 5 | +page_title: Automatically Select the First Row in UI for .NET MAUI DataGrid Using MVVM |
| 6 | +meta_title: Automatically Select the First Row in UI for .NET MAUI DataGrid Using MVVM |
| 7 | +slug: select-first-row-datagrid-mvvm-maui |
| 8 | +tags: datagrid, ui-for-net-maui, mvvm, default-selection, selecteditem |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.1.0 | DataGrid for .NET MAUI | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to select the first row in the [DataGrid](https://docs.telerik.com/devtools/maui/controls/datagrid/overview) component by default using an MVVM approach. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to set a default selected row in the UI for .NET MAUI DataGrid? |
| 24 | +- How to bind the DataGrid's `SelectedItem` property in MVVM? |
| 25 | +- How to programmatically select the first row in a DataGrid? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To select the first row by default, bind the `SelectedItem` property of the DataGrid to a property in the `ViewModel` and set its value to the first item in the collection. |
| 30 | + |
| 31 | +1. Define the `ViewModel`: Create a `SelectedItem` property in the `ViewModel` to hold the selected row. |
| 32 | + |
| 33 | +```csharp |
| 34 | +private MyItem selectedItem; |
| 35 | + |
| 36 | +public ObservableCollection<MyItem> Items { get; set; } |
| 37 | + |
| 38 | +public MyItem SelectedItem |
| 39 | +{ |
| 40 | + get => this.selectedItem; |
| 41 | + set |
| 42 | + { |
| 43 | + if (this.selectedItem != value) |
| 44 | + { |
| 45 | + this.selectedItem = value; |
| 46 | + OnPropertyChanged(nameof(this.SelectedItem)); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public event PropertyChangedEventHandler PropertyChanged; |
| 52 | +protected virtual void OnPropertyChanged(string propertyName) => |
| 53 | + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 54 | +``` |
| 55 | + |
| 56 | +This setup ensures that changes to the `SelectedItem` property automatically notify the UI to refresh. |
| 57 | + |
| 58 | +2. Set the Default Selection: Assign the first item in the collection to the `SelectedItem` property. |
| 59 | + |
| 60 | +```csharp |
| 61 | +public MainViewModel() |
| 62 | +{ |
| 63 | + this.Items = new ObservableCollection<MyItem> |
| 64 | + { |
| 65 | + new MyItem { Name = "Item 1", Description = "First item description" }, |
| 66 | + new MyItem { Name = "Item 2", Description = "Second item description" }, |
| 67 | + new MyItem { Name = "Item 3", Description = "Third item description" } |
| 68 | + }; |
| 69 | + |
| 70 | + this.SelectedItem = this.Items[0]; // Select the first item. |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +3. Bind the ViewModel to the DataGrid: In XAML, bind the `SelectedItem` property of the DataGrid to the `ViewModel` and set the binding mode to `TwoWay`. |
| 75 | + |
| 76 | +```xml |
| 77 | +<telerik:RadDataGrid |
| 78 | + ItemsSource="{Binding Items}" |
| 79 | + SelectedItem="{Binding SelectedItem, Mode=TwoWay}" /> |
| 80 | +``` |
| 81 | + |
| 82 | +This approach ensures that the first row in the DataGrid is selected by default. |
| 83 | + |
| 84 | +## See Also |
| 85 | + |
| 86 | +- [DataGrid Overview](https://docs.telerik.com/devtools/maui/controls/datagrid/overview) |
| 87 | +- [MVVM Pattern in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/xaml/fundamentals/mvvm) |
| 88 | +- [ObservableCollection Class](https://learn.microsoft.com/en-us/dotnet/api/system.collections.objectmodel.observablecollection-1) |
0 commit comments