Raise SelectionChanged event

Hello,

I set programatically the focus to the first row with these lines:

 Grid.SelectedIndex = 0;

 Grid.TableControl.Focus();

In this case the Grid's SelectionChanged event not firing. 

Also not firing when I try navigate to the previous or next search result described here:

https://www.syncfusion.com/kb/9638/how-to-search-and-select-record-in-winforms-datagrid-sfdatagrid

Can you help me please how can I trigger the SelectionChanged event manually (or why not firing automatically)?

Thank you!

BR, László


8 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team August 10, 2022 03:01 PM UTC

Hi SZL,

We have checked the reported issue on our end. By default, SelectionChanged events only listen to the UI and Keyboard interactions. In your scenario, you are performing this programmatically. So, there are no notifications raised for the SelectionChanged event. That’s why the SelectionChanged event does not fire. However, you can get the selection changed notification by raising SfDataGrid.SelectedItems.CollectionChanged event. We have prepared the sample based on your scenario. Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,
Dhanasekar M.


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


Attachment: SfDatagridDemo_4b437ea6.zip


SZ SZL replied to Dhanasekar Mohanraj August 16, 2022 12:17 PM UTC

Thank you I will try it shortly.



SZ SZL replied to Dhanasekar Mohanraj August 17, 2022 07:24 PM UTC

Thank you its working but I have a question:

When I select an another row in the grid, the following Actions executing in the CollectionChanged event (the event raises two times):

- Remove (I think remove selection from the current row)

- Add (I think after selected the new row)

When I delete a row from grid programatically:

- Remove (I think remove selection from the deleted row)

So how can I determine the reason of the Remove action (NotifyCollectionChangedEventArgs.Action)?

I need do some things when the user deleting a row, but I not need to do anything when the user select an another row (and the  CollectionChanged event raising again with Remove Action). 

Thanks!



DM Dhanasekar Mohanraj Syncfusion Team August 18, 2022 02:15 PM UTC

Hi SZL,

We have checked the reported issue on our end. In that, the collection changed event gets a notification for all actions. For example when you select any record event triggered for “Add” and when you select another record the event gets the notification for “Reset” for the selection changed the process and “Remove” for the previously selected record and “Add” for the newly selected record. If you need to get the notifications when you delete the record you check the NotifyCollectionChangedAction.Remove action and inside this, you can do your required process shown below.

sfDataGrid1.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;

private
void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

{

    if(e.Action == NotifyCollectionChangedAction.Remove)

    {

        // You can do the customization here.

    }

}


We have modified the sample based on your scenario. Please find the sample in the attachment and let us know if you have any concerns about this.

Regards,

Dhanasekar M.

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


Attachment: SfDatagridDemo_9811f72c.zip


SZ SZL replied to Dhanasekar Mohanraj August 18, 2022 03:00 PM UTC

Hi,

Thank you for the details, I came to the same conclusion. But my problem is here described in green message (sorry I am beginner of english and a little hard to explain):

 if(e.Action == NotifyCollectionChangedAction.Remove)

    {

        // I cannot determine here the reason of the Remove action because it's occur on row change and row delete too. And I need do some operations only when the user delete a row and I should do nothing when the user select an another row in grid.

    }


Thank you!

BR, SZL



DM Dhanasekar Mohanraj Syncfusion Team August 19, 2022 01:53 PM UTC

Hi SZL,

We have understood your requirement. As we mentioned earlier the NotifyCollectionChangedAction.Remove action occurs in both scenarios. However, you can achieve your requirement by using SfDataGrid.RecordDeleting along with the SfDataGrid.SelectedItems.CollectionChanged event shown below,

this.sfDataGrid1.RecordDeleting += SfDataGrid1_RecordDeleting;
this.sfDataGrid1.SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;

private void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)

{

    if (e.Action == NotifyCollectionChangedAction.Remove)

    {

        var data = deletedItems[0] as OrderInfo;

        // Here you can check whether the deleted record and the selection removed items are same

        if (data.OrderID == (e.OldItems[0] as OrderInfo).OrderID)

        {

        }

    }

}

private List<object> deletedItems;

private void SfDataGrid1_RecordDeleting(object sender, Syncfusion.WinForms.DataGrid.Events.RecordDeletingEventArgs e)

{

    // RecordDeleting event occurs when the record is being deleted from SfDataGrid.

    deletedItems = new List<object>();

    deletedItems.Add(e.Items[0]);

} 


For more information related to record deleting events, please refer to the below user guide documentation link,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/datamanipulation#conditionally-deleting-records

We have modified the sample based on your scenario. Please find the sample in the attachment and let us know if you have any concerns about this.


Regards,

Dhanasekar M.


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


Attachment: SfDatagridDemo__b86facf6.zip

Marked as answer

SZ SZL replied to Dhanasekar Mohanraj August 19, 2022 07:23 PM UTC

Hy,

Thank you! My Grid datasource is an ObservableCollection<Model> and with my delete button I remove the record from the datasource. In this case unfortunatelly the RecordDeleting event not firing, but I succeed to save the deleted row in my own method so its works fine now.

Thank you for help!

BR, SZL



DM Dhanasekar Mohanraj Syncfusion Team August 22, 2022 02:19 PM UTC

Hi SZL,

We are glad that the reported issue was resolved on your side. Please let us know if you need any other details on this. As always, we will be happy to assist you.

Regards,
Dhanasekar M.


Loader.
Up arrow icon