当DataTable绑定到界面显示时,而后台其他线程同时更新DataTable时,会偶然发生此错误,
解决的办法之一是在更新时,添加beginEdit(),更新完以后,添加endEdit()
 


DataTable 内部索引被破坏,5
 
当发生此问题时,可能有二个原因引起的,一个原因是我自已碰到的。
一:当此表格被关联到一些界面控件时,这时候,你在后台需要循环添加记录时(加一条一般不会引发此问题),可能会引起此问题,你在后台添加记录时,需要进行如下操作。
           
            ‘Me.dgHotStep.DataSource
= “”     这是直接把界面控件的DataSoruce设掉
            或
            ‘Me.bsHotStep.DataMember
= “”     这是把中间控件DataSource的数据连接设掉
    能实现一样的效果,
 
            然后,循环添加记录,或做其他数据操作,操作完毕以后,再回复数据绑定。
       
‘Me.dgHotStep.DataSource = Me.bsHotStep
        或
       
Me.bsHotStep.DataMember = Me.dsRecipe.StepHotMixer.TableName
 
,完整的源码如下:
    Private Sub
createHotStep()
        Dim hsRow As
Recipe.StepHotMixerRow
       
‘要清除所有已有数据
       
‘Me.dgHotStep.Rows.Clear()
       
‘Me.dgHotStep.DataSource = “”
       
‘Me.bsHotStep.DataMember = “”
        If
(Me.dsRecipe.StepHotMixer.Count > 0)
Then
            For i
As Integer = Me.dsRecipe.StepHotMixer.Count – 1 To 0 Step
-1
               
Me.dsRecipe.StepHotMixer(i).Delete()
           
Next
            ‘For
Each row1 As Recipe.StepHotMixerRow In
Me.dsRecipe.StepHotMixer
           
‘   
row1.Delete()
           
‘Next
        End
If
        Dim table1 As New
Recipe.StepHotMixerDataTable
        Dim
hotMainList As List(Of String) = EnumProvider._global.getHotMainProcess()
 
        For Each m As String In
hotMainList
           
hsRow =
Me.createHotStepRowByMainProcess(m)
           
If (hsRow IsNot Nothing) Then
 
               
Me.dsRecipe.StepHotMixer.AddStepHotMixerRow(hsRow)
               
‘ Me.dsRecipe.StepHotMixer.DefaultView
 
               
‘如果不是End,则然后同时,新增一条空记录,供用户修改
               
If (m = ConstDefine.cEND)
Then
                   
Exit
For
               
Else
                   
hsRow =
Me.dsRecipe.StepHotMixer.NewStepHotMixerRow()
                   
hsRow.processStep = Me.dsRecipe.StepHotMixer.Count +
1
                   
Me.dsRecipe.StepHotMixer.AddStepHotMixerRow(hsRow)
               
End If
            End
If
       
Next
        ‘Me.dgHotStep.DataSource =
Me.bsHotStep
       
Me.bsHotStep.DataMember =
Me.dsRecipe.StepHotMixer.TableName
    End
Sub

另外一个引起此问题的原因是,当你对此DataTable创建了一个DataView,但此时,其他的线程正在更新此DataTable,就会产生此错误(网上Search到此问题),解决的办法如下:

    lock(Table){
        DataView dv = new
DataView(table)…
        …
    }
估计是更改和创建DataView同时发生,等DataView用完,释放掉以后,就可以更新数据了。
我自已没有碰到过这样的问题,碰到了再研究吧,