New to Telerik UI for Xamarin? Download free 30-day trial

Access DataGrid Cell info on selection changed

Environment

Product Version 2020.3.1106.1
Product DataGrid for Xamarin.Forms

Description

This help article will show you how to access the DataGrid Cell info on SelectionChanged event and SelectionUnito is Cell.

Solution

Inside the SelectionChanged event handler, you're receiving a DataGridSelectionChangedEventArgs object which provides information on the selected item/items depending on the SelectionUnit and SelectionMode.

  • AddedItems - gets a list of the added items to the SelectedItems collection.

In the concrete case, SelectionUnit is "Cell" and for example "SelectionMode is "Single". Since the SelectionUnit is "Cell", the selected item would be of DataGridCellInfo type, not the Person data type.

Here is the DataGrid definition:

<datagrid:RadDataGrid x:Name="dataGrid" 
                          ItemsSource="{Binding GridSource}" 
                          Grid.Row="4" 
                          Grid.ColumnSpan="4" 
                          SelectionChanged="dataGrid_SelectionChanged"
                          SelectionMode="Single"
                          SelectionUnit="Row" />

and the namespace used:

xmlns:datagrid="clr-namespace:Telerik.XamarinForms.DataGrid;assembly=Telerik.XamarinForms.DataGrid"

The SelectionChanged event:

private void dataGrid_SelectionChanged(object sender, DataGridSelectionChangedEventArgs e)
{
    Telerik.XamarinForms.DataGrid.RadDataGrid SO = (Telerik.XamarinForms.DataGrid.RadDataGrid)sender;

    Telerik.XamarinForms.DataGrid.DataGridCellInfo DGR = e.AddedItems.FirstOrDefault() as Telerik.XamarinForms.DataGrid.DataGridCellInfo;
    if(DGR != null)
    {
        string cellInfo = (DGR.Item as Person).Name;
    }         
}

and the used business model and ViewModel:

public class ViewModel
{

    public ViewModel()
    {
        this.GridSource = new ObservableCollection<Person>()
        {
            new Person { Name = "Kiko", Age = 23, Department = "Production" },
            new Person { Name = "Jerry", Age = 23, Department = "Accounting & 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 & Finance"},
            new Person { Name = "Ross", Age = 30, Department = "Marketing" }
        };

    }

    public ObservableCollection<Person> GridSource { get; set; }   
}

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Department { get; set; }
}
In this article