[.NET Framework] - C1FlexGrid.SelectionChanged not raised on ItemsSource change

Posted by: daniel.schoenbach on 20 May 2024, 1:13 pm EST

  • Posted 20 May 2024, 1:13 pm EST - Updated 20 May 2024, 1:19 pm EST

    Hi,

    I’m experiencing an issue with the .NET Framework version 4.6.20233.827 of C1FlexGrid. When AutoGenerateColumns=“False” and SelectionMode=“ListBox”, the C1FlexGrid.SelectionChanged event is not always being raised when the ItemsSource property changes. Similarly, the value of SelectedItems during SelectedItemChanged is unreliable.

    When ItemsSource changes, the value of SelectedItems during SelectedItemChanged hasn’t been updated. When ItemsSource changes to an empty list, the SelectionChanged event isn’t raised at all.

    I’ve attached a minimal reproduction:

    FlexGridIssues.zip

    In my use case, the C1FlexGrid is wrapped in another control with a read-only dependency property. This dependency property is being set when the C1FlexGrid’s selected items change. However, the value is becoming stale and contains items no longer in the collection because I can’t get an accurate picture of SelectedItems during any of the events.

    I’m going to try registering a property changed handler for the SelectedItems property as a workaround, next.

  • Posted 22 May 2024, 6:08 am EST

    Hi Daniel,

    Thanks for reaching out to us.

    1. SelectionChanged and SelectionChanging events: These events fire when the Selection Range updates. For e.x., If you select Sample Data A, then GridView Selection will update to Row->0. Once you update the ItemsSource while selecting the Sample Data B. These events will not fire because there is no change in the Selection Range i.e., it still Row->0.

      If you want to fire SelectionChanged and SelectionChanging events once you change the ItemsSource. then you can handle the ItemsSourceChanged event of FlexGrid as:
    private void ItemsSourceChanged(object sender, EventArgs e)
    {
        GridView.Selection = new CellRange(-1, -1);
        GridView.Selection = new CellRange(0, 0);
    }
    1. SelectedItemChanged event: This event fires on SelectionChanged and ItemsSourceChanged. Once we change the ItemsSource, it is sure that the Items will be updated on the selected range. On ItemsSource Change it fires before the SelectionChanged event. That’s why you are getting the unexpected behavior. To get rid of this issue, you can set a small instance of delay on SelectedItemChanged as:
    private async void GridView_OnSelectedItemChanged(object sender, EventArgs e)
    {
        await Task.Delay(1); //1 ms will be enough
        SelectedItemChanged_SelectedItem.Content = GridView.SelectedItem;
        SelectedItemChanged_SelectedItems.ItemsSource = new ArrayList(GridView.SelectedItems ?? Array.Empty<Person>());
    }

    Please refer the attached modified sample for the same: FlexGridIssues_Fixed.zip

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels