DataGrid selected item is null ? Why?

2 Answers 127 Views
DataGrid
Daniel
Top achievements
Rank 1
Silver
Bronze
Daniel asked on 09 Mar 2022, 12:36 PM

Hi,

  DataGrid selected item is null ? Why?
private void OnResultsDataGridSelectionChanged(object sender, Telerik.XamarinForms.DataGrid.DataGridSelectionChangedEventArgs e)
    {
        var resultsDataGrid = sender as Telerik.XamarinForms.DataGrid.RadDataGrid;
        if (resultsDataGrid != null)
        {
           var selectedItem = resultsDataGrid.SelectedItem.ToString();
           if(selectedItem != null)
           {

           }
        }
    }


 <telerikDataGrid:RadDataGrid Grid.Row="4" Grid.Column="0" x:Name="resultsDataGrid" Margin="10"
                AutoGenerateColumns="False" SelectionChanged="OnResultsDataGridSelectionChanged">

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 11 Mar 2022, 02:27 PM

Hello Daniel,

Since you're using SelectionChanged event handler, the best approach here is to utilize the DataGridSelectionChangedEventsArgs - the event args provides AddedItems collection which holds the newly selected item, here is the modified event handler:

private void OnResultsDataGridSelectionChanged(object sender, Telerik.XamarinForms.DataGrid.DataGridSelectionChangedEventArgs e)
{
    var resultsDataGrid = sender as Telerik.XamarinForms.DataGrid.RadDataGrid;
    if (resultsDataGrid != null)
    {
        var selectedItem = e.AddedItems.First();                
        if (selectedItem != null)
        {

        }
    }
}

Just need to add System.Linq; namespace in order to resolve First() method:

using System.Linq;

I hope I was of help.

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 13 Mar 2022, 02:57 PM

How is done in MVVM ?
0
Yana
Telerik team
answered on 17 Mar 2022, 08:58 AM

Hi Daniel,

If you'd like to get the selected item in a MVVM manner, you would need to bind SelectedItem property of the DataGrid to a property inside the ViewModel of the data item type.  Let's take as a base the example in the DataGrid Selection topic - DataGrid is bound to a collection of Person objects.

First, the ViewModel class should implement INotifyPropertyChanged interface ( we have a base class that implements it called NotifyPropertyChangedBase), then add a SelectedPerson property:

public class ViewModel : Telerik.XamarinForms.Common.NotifyPropertyChangedBase
{
    private Person selectedPerson;

    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 ObservableCollection<Person> People { get; set; }

    public Person SelectedPerson
    {
        get { return this.selectedPerson; }
        set
        {
            if(this.selectedPerson != value)
            {
                this.selectedPerson = value;
                this.OnPropertyChanged();
            }
        }
    }
}

Then, bind SelectedPerson to SelectedItem when defining the DataGrid:

<telerikDataGrid:RadDataGrid x:Name="dataGrid"
    ItemsSource="{Binding People}"
    SelectedItem="{Binding SelectedPerson,Mode=TwoWay}" />

I hope I was of help.

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DataGrid
Asked by
Daniel
Top achievements
Rank 1
Silver
Bronze
Answers by
Yana
Telerik team
Share this question
or