1

我有一个名为“myResults”的列表。当列表至少包含一项时,我正在尝试为列表填充带有信息的 GridView;GridView 被称为“gvMyResults”。但是,有时,在 DataSource 的此更新期间,我收到异常消息“锁定时无法更改边界”,并且在我的 GridView 上显示一个红色 X。我在我的更新周围添加了这个 Dispatcher 包装器,但我仍然看到同样的错误。很难排除故障,因为它不会每次都发生。此外,此 GridView 是 Telerik RadGridView。

有谁知道如何阻止这个“锁定时不能更改边界”错误?

if (myResults.Count > 0) 
{
    Dispatcher.CurrentDispatcher.Invoke(
        DispatcherPriority.Normal, 
        (Action)delegate()
    {
        this.gvMyResults.DataSource = myResults;
    });
}

我也试过这个,但我得到了同样的例外:

_Context.Send(x =>
{
    this.gvMyResults.DataSource = myResults;
}, null);

异常/堆栈跟踪(根据要求):

Type=System.InvalidOperationException
Source=Telerik.WinControls
Message=Bounds cannot be changed while locked.
StackTrace:
    at Telerik.WinControls.RadElement.SetBoundsCore(Rectangle bounds)
    at Telerik.WinControls.RadElement.set_Size(Size value)
    at Telerik.WinControls.RadElement.SetLayoutParams(PointF newOffset, SizeF newSize)
    at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
    at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
    at Telerik.WinControls.UI.RowsContainerElement.ArrangeOverride(SizeF finalSize)
    at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
    at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
    at Telerik.WinControls.UI.ScrollViewElement`1.ArrangeViewElement(RectangleF viewElementRect)
    at Telerik.WinControls.UI.ScrollViewElement`1.ArrangeOverride(SizeF finalSize)
    at Telerik.WinControls.RadElement.ArrangeCore(RectangleF finalRect)
    at Telerik.WinControls.RadElement.Arrange(RectangleF finalRect)
    at Telerik.WinControls.Layouts.ContextLayoutManager.UpdateLayout()
    at Telerik.WinControls.RadElement.UpdateLayout()
    at Telerik.WinControls.UI.GridVisibilityHelper.EnsureRowVisible(GridViewRowInfo rowInfo)
    at Telerik.WinControls.UI.GridTableElement.EnsureRowVisible(GridViewRowInfo rowInfo)
    at Telerik.WinControls.UI.GridTableElement.EnsureCellVisible(GridViewRowInfo rowInfo, GridViewColumn column)
    at Telerik.WinControls.UI.GridTableElement.UpdateViewCore(Object sender, DataViewChangedEventArgs args)
    at Telerik.WinControls.UI.GridTableElement.UpdateView(Object sender, DataViewChangedEventArgs args)
    at Telerik.WinControls.UI.GridTableElement.ProcessTemplateEvent(GridViewEvent eventData)
    at Telerik.WinControls.UI.GridTableElement.Telerik.WinControls.UI.IGridViewEventListener.ProcessEvent(GridViewEvent eventData)
    at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessCollection(GridViewEvent gridEvent, PriorityWeakReferenceList list, GridEventProcessMode processMode)
    at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessEvent(GridViewEvent gridEvent)
    at Telerik.WinControls.UI.GridViewSynchronizationService.NotifyListeners(GridViewEvent gridEvent)
    at Telerik.WinControls.UI.GridViewSynchronizationService.FlushEvents()
    at Telerik.WinControls.UI.GridViewSynchronizationService.DispatchEvent(GridViewEvent gridEvent)
    at Telerik.WinControls.UI.GridViewSynchronizationService.RaiseCurrentChanged(GridViewTemplate template, GridViewRowInfo row, GridViewColumn column, Boolean user)
    at Telerik.WinControls.UI.GridViewTemplate.CollectionView_CurrentChanged(Object sender, EventArgs e)
    at System.EventHandler.Invoke(Object sender, EventArgs e)
    at Telerik.WinControls.Data.RadCollectionView`1.OnCurrentChanged(EventArgs args)
    at Telerik.WinControls.Data.RadCollectionView`1.SetCurrentPositionCore(Int32 newPosition, Boolean forceNotify, CurrentChangeReason reason)
    at Telerik.WinControls.Data.RadDataView`1.SyncCurrent(TDataItem item)
    at Telerik.WinControls.Data.RadDataView`1.RebuildData(Boolean notify)
    at Telerik.WinControls.Data.RadDataView`1.RefreshOverride()
    at Telerik.WinControls.Data.RadDataView`1.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
    at Telerik.WinControls.Data.RadCollectionView`1.source_CollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
    at Telerik.WinControls.Data.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
    at Telerik.WinControls.Data.RadListSource`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    at Telerik.WinControls.Data.RadListSource`1.EndUpdate(Boolean notifyUpdates)
    at Telerik.WinControls.Data.RadListSource`1.Initialize()
    at Telerik.WinControls.Data.RadListSource`1.Bind(Object dataSource, String dataMember)
    at Telerik.WinControls.Data.RadListSource`1.set_DataSource(Object value)
    at Telerik.WinControls.UI.GridViewTemplate.set_DataSource(Object value)
    at Telerik.WinControls.UI.RadGridView.set_DataSource(Object value)
    at MyProject.MyWindow.<>c__DisplayClass2.<DisplayCallerInfo>b__0() 
4

1 回答 1

3

此错误表示网格未处于有效状态。我在代码片段中注意到您异步更新数据源,我想澄清一下,所有 UI 控件都不是整个 Windows 窗体 UI 平台中的线程安全控件。这是MSDN 上的一篇文章,描述了如何制作线程安全的 Winforms UI 应用程序。这意味着 RadGridView 也不是线程安全的,并且不能在主 UI 线程之外使用。您应该使用Invoke来更新跨线程场景中的控件。例如:

if (gvMyResults.InvokeRequired)
{
   gvMyResults.Invoke(new MethodInvoker(() => { this.gvMyResults.DataSource = myResults; }));
}
else
{ 
 this.gvMyResults.DataSource = myResults;
}
于 2013-08-02T13:05:18.277 回答