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ó
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.
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!
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; { 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.
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
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; { 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.
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
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.