将数据源分配给绑定源会引发异常

时间:2012-07-03 14:29:14

标签: c# winforms datagridview

我的代码中有以下逻辑。

Initialize(){
    DataGridView view = new DataGridView();
    view.BindingSource = bs;
    bs.dataSource = dataTable;

    //Fill Data Table using Adapter.
    da.fill(dataTable);
}

CallMeEveryFewMinutes(DataTable dataTable){
    List<String> changed = findChangedOjbects();
    // Fill datatable2 with changed objects.
    da2.fill(datatable2, changed)     

    Refresh(dataTable, datatable2);
    // dataTable is now refreshed. Bind it again so changes are reflected.

    // ********** PROBLEM AREA ***************
    // once in a while it throws the below exception.
    bs.dataSource = dataTable;
} 

不仅如此,它还会为datagridview.sort(...)抛出相同的异常 - 偶尔...

任何指针都会非常有用。

  

System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---&GT; System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。   参数名称:rowIndex      在System.Windows.Forms.DataGridView.GetCellDisplayRectangle(Int32 columnIndex,Int32 rowIndex,Boolean cutOverflow)      在System.Windows.Forms.DataGridView.GetCellAdjustedDisplayRectangle(Int32 columnIndex,Int32 rowIndex,Boolean cutOverflow)      在System.Windows.Forms.DataGridView.InvalidateCellPrivate(Int32 columnIndex,Int32 rowIndex)      在System.Windows.Forms.DataGridView.OnCellCommonChange(Int32 columnIndex,Int32 rowIndex)      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender,ListChangedEventArgs e)      在System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)      在System.Windows.Forms.CurrencyManager.CancelCurrentEdit()      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.CancelRowEdit(Boolean restoreRow,Boolean addNewFinished)      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowValidating(DataGridViewCellCancelEventArgs e)      在System.Windows.Forms.DataGridView.OnRowValidating(DataGridViewCell&amp; dataGridViewCell,Int32 columnIndex,Int32 rowIndex)      在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex,Int32 rowIndex,Boolean setAnchorCellAddress,Boolean validateCurrentCell,Boolean throughMouseClick)      在System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell值)      在System.Windows.Forms.DataGridView.OnClearingRows()      在System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean recreateNewRow)      在System.Windows.Forms.DataGridView.RefreshColumnsAndRows()      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.DataSourceMetaDataChanged()      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.ProcessListChanged(ListChangedEventArgs e)      在System.Windows.Forms.DataGridView.DataGridViewDataConnection.currencyManager_ListChanged(Object sender,ListChangedEventArgs e)      在System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)      在System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender,ListChangedEventArgs e)      在System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)      在System.Windows.Forms.BindingSource.ResetBindings(Boolean metadataChanged)      在System.Windows.Forms.BindingSource.SetList(IList list,Boolean metaDataChanged,Boolean applySortAndFilter)      在System.Windows.Forms.BindingSource.ResetList()      在System.Windows.Forms.BindingSource.set_DataSource(对象值)      在C:\ Project \ Project_1.9 \ WindowsFormsApplication \ ViewHelper \ MainForm \ FormHelper.cs中的Caddie.ViewHelper.MainForm.FormHelper.refreshWorker_RunWorkerCompleted(Object sender,RunWorkerCompletedEventArgs e):第373行      在System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)      在System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)

2 个答案:

答案 0 :(得分:0)

我的假设是,由于您修改了数据并将其重新绑定到控件,因此正在调用一些侦听网格本身的事件。无论是SelectionChanged还是RowRemoved,您基本上都是在尝试针对没有数据的Gridview运行这些调用。

检查您的事件侦听器,看看您是否有任何调用方法来尝试访问网格中的数据。您甚至可以在程序中放置一些断点,以查看这些方法的调用顺序。重新绑定发生时,请注意调用代码的顺序。

为了解决这个问题,在事件监听器上检查dgv.Rows.Count是否等于0。

答案 1 :(得分:0)

我从

中找到了一个非常好的提示

http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/279d3c64-c1c2-4927-b0cc-79866c09e035/

“我想出来了,但发布在另一个帖子上。基本上已经制作了自己的数据索引的货币管理器正在逐渐消失。在我的收集对象中,我允许自己传递datagridview对象所以,如果有任何东西被删除,我可以调用bindingmanager.resetcurrentbindings。如果你在删除后重置绑定,那么这将避免这个错误。“

所以,我稍微改变了我的代码,问题(看起来像)现在已经解决了。

Initialize(){
    DataGridView view = new DataGridView();
    view.BindingSource = bs;
    bs.dataSource = dataTable;

    //Fill Data Table using Adapter.
    da.fill(dataTable);
}

CallMeEveryFewMinutes(DataTable dataTable){
    List<String> changed = findChangedOjbects();
    // Fill datatable2 with changed objects.
    da2.fill(datatable2, changed)     

    Refresh(dataTable, datatable2);
    // dataTable is now refreshed. Bind it again so changes are reflected.

    // ********** PROBLEM AREA -- SOLVED ***************
    // once in a while it throws the below exception.
    //bs.dataSource = dataTable; don't rebind the same datatable, but rather reset the bindings.
    bs.ResetBindings(false);
}