We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Exception when trying to set SelectedItem when DataGrid is grouped

Hi,

i got a Null Reference Exception when trying to set the SelectedItem when the DataGrid is grouped.


var item = wiFiDataGrid.View.Records.Where(x => ((WiFi_Model)x.Data).BSSID == App.SelectedNetwork.Network.BSSID).FirstOrDefault();
wiFiDataGrid.SelectedIndex = wiFiDataGrid.View.Records.IndexOf(item);



 bei Syncfusion.UI.Xaml.Grid.GridIndexResolver.ResolveToRowIndex(SfDataGrid dataGrid, Object recordItem)
   bei Syncfusion.UI.Xaml.Grid.GridSelectionController.ProcessSelectedItemChanged(SelectionPropertyChangedHandlerArgs handle)
   bei Syncfusion.UI.Xaml.Grid.GridBaseSelectionController.HandleSelectionPropertyChanges(SelectionPropertyChangedHandlerArgs handle)
   bei Syncfusion.UI.Xaml.Grid.SfDataGrid.OnSelectedItemChanged(DependencyPropertyChangedEventArgs args)
   bei Syncfusion.UI.Xaml.Grid.SfGridBase.OnSelectedItemChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
   bei System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   bei System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   bei System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   bei System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   bei System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   bei System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   bei Syncfusion.UI.Xaml.Grid.SfGridBase.set_SelectedItem(Object value)

5 Replies

DY Deivaselvan Y Syncfusion Team March 25, 2019 12:05 PM UTC

Hi Helge, 

Thank you for contacting Syncfusion Support. 

We have analyzed the reported “Null reference exception throws when setting SelectedItem with grouping” issue. But we are unable to reproduce the reported issue from our end. We have prepared simple sample based on the reported scenario and please find it from the below link 

Sample Link: 

Also, we have handled necessary null checks in the reported ResolveToRowIndex method and we are not facing any kind of null exception in the given stacktrace. If still issue exists at your end, could you please revert us with the below details? 
1.      If any customization done in your end, kindly share that details 
2.      Settings of sfdatagrid at your end 
3.      Details of how you are populating datas to the grid. 
4.      If possible, please share us the modified sample  

It will help us to investigate further and provide you with appropriate solution at the earliest. 

Regards,
Deivaselvan 



HK Helge Keck March 25, 2019 05:12 PM UTC

Thanks for responding,

if i use your sample and change iut to:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var record = this.sfGrid.View.Records.Where(rec => (rec.Data as UserInfo).UserId == 1004).FirstOrDefault();
    this.sfGrid.SelectedIndex = this.sfGrid.View.Records.IndexOf(record);
}

i have the exact same Problem here, it selects the wrong item

and, after restarting the APP it still selects the wrong item, but another one


if i set the SelectedIndex it doesnt throw an exception, only with SelectedItem

Version 16.4460.0.42


DY Deivaselvan Y Syncfusion Team March 26, 2019 10:18 AM UTC

Hi Helge, 

Thanks for the update. 

We have analyzed your queries and please find answers below 

Query 
Answer 
Setting SelectedIndex selects the wrong item 
In your code, you have set the wrong index for SelectedIndex. Since “sfGrid.View.Records.IndexOf(record)” returns the position of record in the recordlist and it does not include Groups in the view. So you need to set the actual position of the record in the view by using helper methods as like below code snippet 

var record = this.sfGrid.View.Records.Where(rec => (rec.Data as UserInfo).UserId == 1004).FirstOrDefault(); 
var rowIndex = this.sfGrid.ResolveToRowIndex(record.Data); 
this.sfGrid.SelectedIndex = this.sfGrid.ResolveToRecordIndex(rowIndex); 


Setting SelectedItem throws exception 
From the provided code snippet, we suspect that you have set the recordEntry directly to the SelectedItem which will throw exception. You can resolve the reported exception by setting row data as the SelectedItem as like below code snippet 

var record = this.sfGrid.View.Records.Where(rec => (rec.Data as UserInfo).UserId == 1004).FirstOrDefault(); 
this.sfGrid.SelectedItem = record.Data; 



Please find sample for the same from the below link 
Sample Link: 

Please let us know, if you need any further assistance on this. 

Regards, 
Deivaselvan 



HK Helge Keck March 26, 2019 01:00 PM UTC

thank you, it works now


DY Deivaselvan Y Syncfusion Team March 27, 2019 06:02 AM UTC

Hi Helge, 

You are welcome.
Please feel free to contact us in case of any further difficulty. We'll be glad to assist you. 
 
Regards, 
Deivaselvan 


Loader.
Live Chat Icon For mobile
Up arrow icon