Completed
Last Updated: 20 Oct 2023 12:49 by ADMIN
Release LIB 2023.3.1023 (23 Oct 2023)
Richard
Created on: 07 Jul 2023 12:07
Category: GridView
Type: Bug Report
0
GridView: InvalidOperationException is thrown when displaying RadGridView and column groups on more than one UI thread
*Original title: Cross thread exception with empty hierarchical headers*

I already have a work-around and I don't have time to craft an example.

 

When running two dispatchers in different threads, ie launching the following code twice:

public void LaunchInThread()
        {
            Thread _thread = new Thread(() =>
            {
              Application app = new Application();
              app.Run(new Window());
            });
            _thread.SetApartmentState(ApartmentState.STA);
            _thread.Start();
        }

And setting column hierarchical column headers to some but not all columns by code, ie:

if( ! first_column){
   column.ColumnGroupName = "groupName";
   if (radGridView.ColumnGroups.FirstOrDefault(x => x.Name == "groupName") != null) return;
   radGridView.ColumnGroups.Add(new GridViewColumnGroup { Header = "groupName", Name = "groupName" });
}
When the radGridView is displayed on the second thread, the code will crash with a cross-thread access validation exception, on GridViewColumnGroup.Name

My guess is that when no header is set, by default an static empty GridViewColumnGroup is reused. And as it's non-freezable it crashes.

2 comments
Richard
Posted on: 24 Jul 2023 00:12

Hi Martin.

Yes, it works. Generating new empty groups is the workaround I was using.

Thanks for taking the time to confirm the issue.

 

ADMIN
Martin Ivanov
Posted on: 14 Jul 2023 11:45

Hello Richard,

Thank you for the provided information. I can confirm that this is an issue with the GridView column groups feature. That is why I have updated the status of the item accordingly and updated your Telerik points.

Your guess is correct. There is a static field in the GridViewColumnGroup called EmptyGroup that is used in some situations (like for the columns that don't have groups assigned) which is initialized on the UI thread that first access the GridViewColumnGroup class. Because of that any other thread that tries to access the static field causes issues.

To work this around, you can re-initialize the value of the internal static EmptyGroup field of the GridViewColumnGroup class, when the new thread is initialized, just before initializing the RadGridView instance. For example:

public WindowOpenedOnANewThread()
{
	FieldInfo emptyGroupField = typeof(GridViewColumnGroup).GetField("EmptyGroup", BindingFlags.Static | BindingFlags.NonPublic);
	emptyGroupField.SetValue(null, new GridViewColumnGroup() { Name = string.Empty, Header = string.Empty });

	InitializeComponent();
}

Can you try this and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.