GridView更新时“在锁定时无法更改边界”

时间:2013-07-30 13:46:05

标签: c# winforms telerik radgridview

我有一个名为“myResults”的列表。当列表包含至少一个项目时,我正在尝试为列表填充GridView w / info; 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() 

1 个答案:

答案 0 :(得分:2)

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

if (gvMyResults.InvokeRequired)
{
   gvMyResults.Invoke(new MethodInvoker(() => { this.gvMyResults.DataSource = myResults; }));
}
else
{ 
 this.gvMyResults.DataSource = myResults;
}