none
Find First record in a DefaultView RRS feed

  • Question

  • I am using the following code to return row number of the first record in a table that matches the filter .  I now need to do the same thing to a filtered view (mobjDS.Tables(0).DefaultView.RowFilter = FilterBy) but the tools do not seem to be the same.

    What is the best way to do this?

     

    Public Function FindFirst(ByVal strFilter As String) As Boolean
          '// strFilter Exp: "[LastName] = 'Smith' AND [FirstName] = 'John'"
          Dim Result As Boolean = False
          Try
            Dim dataRow() As DataRow = mobjDS.Tables(0).Select(strFilter)
            If dataRow IsNot Nothing Then
               mintFilterCount = dataRow.Length
               If mintFilterCount > 0 Then
                  mintIndex = mobjDS.Tables(0).Rows.IndexOf(dataRow(0))
                  mobjRow = mobjDS.Tables(0).Rows(mintIndex)
                  Result = True
               End If
            End If
          Catch ex As Exception
             Throw New System.Exception("clsRecordset::FindFirst, " & ex.Message)
          End Try
          Return Result
       End Function

    Wednesday, September 29, 2010 2:33 PM

Answers

  • Sorry Morgan, I misunderstood what you were looking for. Rather than finding the index into the DataTable, you would like to find the index into the DefaultView? There may be a LINQ way of doing this, but I'm not familiar enough with LINQ to even try.

    This will work (assuming I'm still not misunderstanding you!):

    ' Forgive me if the VB syntax is a bit off, I used a conversion tool
    
    Dim dataRow() As DataRow = mobjDS.Tables(0).Select(strFilter)
    
    For mintIndex = 0 To mobjDS.Tables(0).DefaultView.Count - 1
      If mobjDS.Tables(0).DefaultView(mintIndex ).Row.Equals(dataRow(0)) Then
       Exit For
      End If
    Next mintIndex 
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, September 30, 2010 4:30 PM

All replies

  • This will work:

    If mobjDS.Tables(0).DefaultView.Count > 0 Then
      Dim DataRow row = mobjDS.Tables(0).DefaultView(0).Row
      mintIndex = mobjDS.Tables(0).Rows.IndexOf(row)
    End If
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, September 30, 2010 4:56 AM
  • This will work:

    If
     mobjDS.Tables(0).DefaultView.Count
     > 0 Then
    
     Dim
     DataRow row = mobjDS.Tables(0).DefaultView(0).Row
     mintIndex = mobjDS.Tables(0).Rows.IndexOf(row)
    End
     If
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    I don't believe that this will accomplish what I am looking for.  I am trying to find the first row that matches the search criteria, in table that has had a RowFilter applied.

    Your example will return the index of the first DefaultView row in the mobjDS.Tables(0).
    My problem is that .Select(strFilter) and Rows.IndexOf(row) are not a members of DefaultView(0).Row.
    Thursday, September 30, 2010 12:16 PM
  • Sorry Morgan, I misunderstood what you were looking for. Rather than finding the index into the DataTable, you would like to find the index into the DefaultView? There may be a LINQ way of doing this, but I'm not familiar enough with LINQ to even try.

    This will work (assuming I'm still not misunderstanding you!):

    ' Forgive me if the VB syntax is a bit off, I used a conversion tool
    
    Dim dataRow() As DataRow = mobjDS.Tables(0).Select(strFilter)
    
    For mintIndex = 0 To mobjDS.Tables(0).DefaultView.Count - 1
      If mobjDS.Tables(0).DefaultView(mintIndex ).Row.Equals(dataRow(0)) Then
       Exit For
      End If
    Next mintIndex 
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Thursday, September 30, 2010 4:30 PM
  • Thanks, this was exactly what I was looking for.
    Friday, October 1, 2010 12:56 PM
  • You're welcome, Morgan. Glad I could help! =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, October 2, 2010 4:14 PM