BindingSource.Find在DataSet中搜索值(“表”)

时间:2018-05-01 18:48:50

标签: .net vb.net

我正在尝试查找DataSet.Tables("TableName")BindingSource中包含的值。

 'Going through each row in my BindingSource
 For Each row As DataRowView In myBindingSource
      Dim IDNum As Integer = myBindingSource.Find("myIDNum", DataSet.Tables("TableName").Rows("myIDNum"))

      'If IDNum has been found
      If IDNum >= 0 Then
           If FACID("ColumnName").ToString = "Something" Then
                DirectCast(myBindingSource(row), DataRowView)("Column") = "Something else"
           End If
      'If IDNum has not been found
      Else
           myBindingSource.AddNew()
           'Other stuff
      End If
 Next

我在第三行遇到了很多麻烦:

 Dim IDNum As Integer = myBindingSource.Find("myIDNum", DataSet.Tables("TableName").Rows("myIDNum"))

有谁知道如何使这条线工作?我不确定如何使用DataSet.Table中的行执行.find。

修改: 经过第二次看,我认为为了在BindingSource中找到我的DataSet值,它看起来应该如下所示,但这一行的结构是错误的:

  Dim IDNum As Integer = myBindingSource.Find(DataSet.Tables("TableName").Columns("myIDNum"), row(myIDNum))

1 个答案:

答案 0 :(得分:0)

For循环的这一部分:

IDNum As DataRowView

紧接着是:

Dim IDNum As Integer

那是试图给两个变量赋予相同的名称。 不要这样做。根据事物的使用方式,我认为你需要一个不同的循环变量名称。将其与问题中的问题行结合起来:

For Each row As DataRowView In myBindingSource
    Dim IDNum As Integer = ImportBS.Find("myIDNum", row("myIDNum"))

小心这一点。这段代码的结果是一个索引,而不是一个ID号,这些不是一回事!