这个VB.NET代码的逻辑错误是什么

时间:2012-10-21 12:44:15

标签: vb.net linq datatable

我有这个简单的VB.NET功能:

Dim endval = Convert.ToInt16(googleXMLdocument...<s:currentItemCount>.Value) - 1
For counter = 0 To endval
  Dim seller = googleXMLdocument...<s:name>(counter).Value
  Dim containsValue = ToBeIgnored.AsEnumerable().Any(Function(r) r.Field(Of String)("Ignore") = seller) 
  If containsValue Then
    Continue For
  End If
  row = GoogleResults.NewRow()
  GoogleResults.Rows.Add(row)
  GoogleResults.Rows(counter)("Seller") = seller 'sometimes this line throws an exception there is no row at position x
Next

在最后一行中,我有时会遇到异常there is no row at position x。什么可能导致这种情况?

2 个答案:

答案 0 :(得分:2)

您的计数器变量看起来与GoogleResults表的行数不同。

我猜你正在寻找这样的东西:

GoogleResults.Rows(GoogleResults.Rows.Count - 1)("Seller") = seller

或更直接:

row("Seller") = seller

答案 1 :(得分:1)

For循环的最后两行应该像这样重写:

row("Seller") = seller;
GoogleResults.Rows.Add(row)

添加行后,可能会导致触发不必要的事件。