how to pass 2 values to a table.defaultview.find() method I need to position on a row,and my table has two indexes field

Denis Gagnon 0 Reputation points
2023-04-25T21:33:04.3333333+00:00

Hello in visual basic i have a table with no Primary key, it's indexes on two field, but when I'm trying to position on a row with the table.defaultview.find() it won't take two values, so I'm trying to combine my values in one variable but it's rejected as i have only one values thanks for your help

 LocalisationTable.DefaultView.Sort = "LocalId,ItemId"

        criteria = ("LocalId ='" + cboLocalId.Text + "'And itemId = '" + cboItemId.Text + "'")
 SavedRow = LocalisationTable.DefaultView.Find(Criteria)
.NET F#
.NET F#
.NET: Microsoft Technologies based on the .NET software framework.F#: A strongly typed, multi-paradigm programming language developed by the F# Software Foundation, Microsoft, and open contributors.
97 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leo 0 Reputation points
    2023-04-26T09:53:29.6933333+00:00

    Hey! In this case, you can create a new column in your DataTable which combines the two fields and then use the DataView to find the row based on the new combined column value. Here's how you can achieve that:

    1. Add a new column to your DataTable, which will store the combined values of the two fields you want to search on.
    2. Iterate through the rows in the DataTable and update the new column with the combined values.
    3. Set the Sort property on the DataView to use the new column.
    4. Use the DataView's Find method to locate the row based on the combined value.
    ' 1. Add a new column for the combined values
    LocalisationTable.Columns.Add("CombinedField", GetType(String))
    
    ' 2. Iterate through the rows and update the new column with the combined values
    For Each row As DataRow In LocalisationTable.Rows
        row("CombinedField") = row("LocalID").ToString() & "_" & row("ItemID").ToString()
    Next
    
    ' 3. Set the Sort property on the DataView
    Dim dataView As DataView = LocalisationTable.DefaultView
    dataView.Sort = "CombinedField"
    
    ' 4. Use the DataView's Find method to locate the row based on the combined value
    Dim value1 As String = "LocalID"
    Dim value2 As String = "ItemID"
    Dim combinedValue As String = value1 & "_" & value2
    
    Dim rowIndex As Integer = dataView.Find(combinedValue)
    
    If rowIndex <> -1 Then
        Dim foundRow As DataRowView = dataView(rowIndex)
        ' Perform your desired action with the found row
    Else
        ' The combined value was not found in the DataView
    End If
    
    
    

    This code will create a new column "CombinedField" in your LocalisationTable, fill it with combined values of "LocalID" and "ItemID" separated by an underscore (_), and then use the DataView's Find method to locate the row based on the combined value. Is that what you're looking for more or less?

    0 comments No comments