This is a migrated thread and some comments may be shown as answers.

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

3 Answers 60 Views
GridView
This is a migrated thread and some comments may be shown as answers.
intellitechcorporation
Top achievements
Rank 1
intellitechcorporation asked on 18 Feb 2020, 04:41 AM

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.

 

 

3 Answers, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 20 Feb 2020, 03:10 PM

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.
0
intellitechcorporation
Top achievements
Rank 1
answered on 03 Mar 2020, 08:41 PM

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.

0
Nadya | Tech Support Engineer
Telerik team
answered on 06 Mar 2020, 11:27 AM

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.
Tags
GridView
Asked by
intellitechcorporation
Top achievements
Rank 1
Answers by
Nadya | Tech Support Engineer
Telerik team
intellitechcorporation
Top achievements
Rank 1
Share this question
or