RadGridView Auto-Selecting Item When Item Is Added/Removed In ObservableCollection

4 posts, 1 answers
  1. intellitechcorporation
    intellitechcorporation avatar
    30 posts
    Member since:
    Jan 2011

    Posted 17 Feb Link to this post

    Having a bizarre issue with RadGridView where it seems to randomly be selecting items in the grid when the underlying ObservableCollection is changed.  My desired behavior is the "CurrentItem" becoming null when the selected item is removed from the underlying collection, and obviously for the GridView not to select an item when added when the selection is blank.  My code is as follows:

    radGridView1.AutoExpandGroups = true;
                var gd = new GroupDescriptor();
                gd.GroupNames.Add("RandomProp1", ListSortDirection.Ascending);
                radGridView1.GroupDescriptors.Add(gd);
                var sd1 = new SortDescriptor("RandomProp2", ListSortDirection.Descending);
                radGridView1.SortDescriptors.Add(sd1);
                var sd2 = new SortDescriptor("RandomProp3", ListSortDirection.Ascending);
                radGridView1.SortDescriptors.Add(sd2);
                radGridView1.MultiSelect = false;
                radGridView1.DataBindingComplete += RadGridView1_DataBindingComplete;
                radGridView1.DataSource = App.MasterList;

    private void RadGridView1_DataBindingComplete(object sender, Telerik.WinControls.UI.GridViewBindingCompleteEventArgs e)
            {
                Log.WriteLine("RadGridView_DataBindingComplete");
                radGridView1.CurrentRow = null;
                radGridView1.CurrentRowChanged += RadGridView1_CurrentRowChanged;
            }

     

    When the underlying collection is being changed, I'm doing the following:

    BeginInvoke on the main thread

    Locking the collection

    Updating items as such:

    DynamicCollection[i] = new CustomObject(fillerProperties);

     

    Is the above possibly the issue?  I know this is the correct way to update ObservableCollections in WPF when bound to a GridView, not sure if WinForms behaves differently.

     

     

  2. Answer
    Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 20 Feb Link to this post

    Hello Kyle,

    If I understand you correctly you want to deselect an item when another item is added or removed. You can set the SelectLastAddedRow property to false in order to prevent selecting the last added row. 

    this.radGridView1.MasterTemplate.SelectLastAddedRow = false;

    In order to achieve similar behavior when deleting rows, you should subscribe to UserDeletingRow and in CurrentRowChanged you can set CurrentRow to be null. Please refer to the following code snippet:

    private void RadGridView1_UserDeletingRow(object sender, GridViewRowCancelEventArgs e)
    {
        
        this.radGridView1.CurrentRowChanged += this.RadGridView1_CurrentRowChanged1;
    }
    
    private void RadGridView1_CurrentRowChanged1(object sender, CurrentRowChangedEventArgs e)
    {
        this.radGridView1.CurrentRowChanged -= this.RadGridView1_CurrentRowChanged1;
        this.radGridView1.CurrentRow = null; 
    }
    

    I hope this helps. Should you have any other questions do not hesitate to ask.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  3. intellitechcorporation
    intellitechcorporation avatar
    30 posts
    Member since:
    Jan 2011

    Posted 03 Mar in reply to Nadya Link to this post

    Nadya,

    Thank you so much, and sorry for the delayed response!  This appears to have worked perfectly.  I also went down the whole rabbit hole of ObservableCollection vs BindingList, and while there doesn't seem to be any significant difference, I did switch to BindingList just based on the general message that BindingList is for WinForms and ObservableCollection is for WPF.

  4. Nadya
    Admin
    Nadya avatar
    378 posts

    Posted 06 Mar Link to this post

    Hello Kyle,

    I am glad that the suggested solution helps and you managed to get your project works correctly. Do not hesitate to contact us if you have other questions.

    Regards,
    Nadya
    Progress Telerik

    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top