WPF | Datagrid | new fix for selecteditems does not work | Selection is not updated/saved

Hi Syncfusion

The recent changes you have made to SelectedItems does not work properly as the Selection itself is not saved to the binding Observable collection in the ViewModel, leaving the collection to stay at count 0.

I am running on the newest NuGet version for .NET 4.8 WPF

7 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team January 15, 2021 04:33 PM UTC

Hi Hadi Salameh,

Thank you for contacting Syncfusion support.

We are little unclear with your scenario. Based on provided we suspect that SelectedItems property not properly show selected item in SfDataGrid. We have checked the reported issue
“Selection is not updated/saved in SfDataGrid” unable to replicate the issue from our end. it is working fine as expected. Please find the tested sample and video demo from our end in the below link,

Sample link: https://www.syncfusion.com/downloads/support/forum/161511/ze/Sample-1254939473 
Can you please share us below things?       
        1. Brief replication procedure/video illustration of the reported issue
        2.
Could you please provide more details about your scenario with illustrations?
        3. Which fix is not worked for SelectedItems in SfDataGrid

if you still facing the same issue? If yes, please modify the sample based on your scenario. 

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S
 



HA Hadi January 21, 2021 01:14 PM UTC

You are creating your own Dependency property SelectedItems, which is a bit cheating in my opinion. I could have done that myself without the fix. It would have been better if I could just use the SfDatagrid SelectedItems ...


VS Vijayarasan Sivanandham Syncfusion Team January 22, 2021 01:41 PM UTC

Hi Hadi Salameh,

Thanks for the update.

Based on provided information we have checked the reported issue “Selection is not updated/saved in SfDataGrid” and unable to replicate the issue from our end. it is working fine as expected. Please find the tested sample and video demo from our end in the below link,

Sample link: https://www.syncfusion.com/downloads/support/forum/161511/ze/SelectedItems1882667247 
Video Link: https://www.syncfusion.com/downloads/support/forum/161511/ze/SelectedItemBindedViewModelProperty-630995565

We have checked with Latest Syncfusion product version 18.4.0.35. 
Can you please share us below things?       
        1. Brief replication procedure/video illustration of the reported issue
        2. Could you please provide more details about your scenario with illustrations?
        3. Provide the code snippet related customization in SfDataGrid

if you still facing the same issue? If yes, please modify the sample based on your scenario. 

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S 


Marked as answer

HA Hadi January 26, 2021 08:30 AM UTC

Why does the Observable Collection has to be of type Object and not just Person?


VS Vijayarasan Sivanandham Syncfusion Team January 27, 2021 03:58 PM UTC

Hi Hadi,

Thanks for the update.

Based on provided information we did not know the underline collection of ItemSource. So, the SfDataGrid  SelectedItems property is defined as an Object. In this getting the Selected Item after converting the SelecteItems to underlineCollection in your application.

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

Regards,
Vijayarasan S
 



FR Frank replied to Vijayarasan Sivanandham February 22, 2023 03:03 PM UTC

Dear  Vijayarasan,


Sample link: https://www.syncfusion.com/downloads/support/forum/161511/ze/Sample-1254939473 


in the xaml this line yells at me:

 local:SfDataGridHelper.SelectedItems="{Binding SelectedItems, Mode=TwoWay}">


And running it I get:

System.NullReferenceException

for this section:

 sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>

            {

                SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);

            };

Do you have any views on this?

frank





VS Vijayarasan Sivanandham Syncfusion Team February 23, 2023 08:07 PM UTC

Frank,

We have recently made some changes to the DataGrid Selection. For more information on this change, please refer to the release notes linked below.

Release Notes Link: https://help.syncfusion.com/wpf/release-notes/v20.3.0.49?type=all#sfdatagrid-bug-fixes


We only initialize the SelectedItems collection after the control is initialized to provide the proper binding support for the collection. In your scenario, you are trying to access the SelectedItems before the control is loaded, which is why the reported problem occurs.

To overcome this issue, we suggest that you either initialize the SelectedItems before accessing it or use the Dispatcher. Refer to the code snippet below.

private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)

{

    var sfDataGrid = d as SfDataGrid;

    if (sfDataGrid == null)

        return;

    //SfDataGridHelper.SelectedItems property updated based on SfDataGrid.SelectedItems Collectionchanged event. 

 

    ////Solution 1:Here initialize the SelectedItems before accessing

    //if(sfDataGrid.SelectedItems == null)

    //{

    //    sfDataGrid.SelectedItems = new ObservableCollection<object>();

    //}

 

    //sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>

    //{

    //    SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);

    //};

 

    //Solution 2: Here using Dispatcher

    sfDataGrid.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, new Action(() =>

    {

        //This event call after control loaded

        sfDataGrid.SelectedItems.CollectionChanged += (sender, e) =>

        {

            SfDataGridHelper.SetSelectedItems(sfDataGrid, sfDataGrid.SelectedItems);

        };

 

    }));

}


Find the modified sample in the attachment.


Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Attachment: Modified_Sample_b3f75279.zip

Loader.
Up arrow icon