Results 1 to 7 of 7

Thread: Move to a specified record in a datatable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Move to a specified record in a datatable

    Hi I can find the datarow I want like so

    VB Code:
    1. Dim pkey(0) As DataColumn
    2.             pkey(0) = ds.Tables("ContactsCom").Columns(0)
    3.             ds.Tables("ContactsCom").PrimaryKey = pkey
    4.  
    5.             Dim foundRow As DataRow
    6.             Dim findTheseVals(0) As Object
    7.             findTheseVals(0) = 11
    8.             foundRow = ds.Tables("ContactsCom").Rows.Find(findTheseVals)
    9.             If Not (foundRow Is Nothing) Then
    10.                 MsgBox(foundRow(1).ToString())
    11.             End If

    A simple question but how do I move to that datarow in the datatable, so all the bound controls show the value I have just found.
    eg ds.Tables("ContactsCom").Move(11)

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Move to a specified record in a datatable

    Okay have this, but there must be a better way to skin the cat

    VB Code:
    1. While Me.BindingContext(ds, "ContactsCom.ContactID").Current() <> 11
    2.                 Me.BindingContext(ds, "ContactsCom").Position += 1
    3.             End While

    There must be a way to find the position of the record I want and then set it, rather than iterating through the records.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Move to a specified record in a datatable

    Use the DataView class.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    262

    Re: Move to a specified record in a datatable

    Sorry have had a look at the dataview class and can't see how it would help, sure I must be missing something.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Move to a specified record in a datatable

    Dim dv As DataView
    dv = New DataView(ds.Tables(0))
    dv.RowFilter = "FieldName = " & findTheseVals(0)

    Then bind to the dataview.

    Note that, if you make a change while bound to dataview, it won't be updated to the database.

  6. #6
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Move to a specified record in a datatable

    You could also use .Find.

  7. #7
    Junior Member
    Join Date
    Jan 2006
    Posts
    18

    Re: Move to a specified record in a datatable

    The problem I was having was finding values in a datatable instead of going to the database and running a query. So I finally figured it out from the above post buts heres a nicer version:

    VB Code:
    1. 'Code to find a value in a datatable
    2. Dim dv As DataView = New DataView(DataTable) 'Set the dataview to the table to search
    3. Dim rowValue As Integer = Nothing 'The value found
    4.        
    5. 'Filter the dataview
    6. dv.RowFilter = "ColumnNameToSearch = " & ValueToFind
    7.  
    8. 'Make sure a record was found
    9. If dv.Count > 0 Then
    10.       'Get the value from the record found
    11.       rowValue = dv.Item(0).Row("ColumnNameToGetValueFrom")
    12.  
    13.       MessageBox.Show("The value found is " & rowValue)
    14.  
    15. Else
    16.       MessageBox.Show("No records found!")
    17. End If
    18.  
    19.  
    20.  
    21.  
    22. 'To select a current record and show the values on the binding control
    23. Dim rowPosition As Integer = Nothing 'The position of the value in the datatable
    24.  
    25. 'Get the row position in the datatable
    26. rowPosition = BindingSource.Find("ColumnNameToSearch", ValueToFind)
    27.  
    28. 'Make sure a row was fouind and select it
    29. If rowPosition = -1 Then
    30.       MessageBox.Show("No records found!")
    31. Else
    32.       BindingSource.Position = rowPosition
    33. End If

    Just remember when you want to find a text value with
    dv.RowFilter = "ColumnNameToSearch = " & ValueToFind

    the filter must look like this "ColumnName = 'Text' "

    without the ' you will get a column doesn't exist error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width