Results 1 to 6 of 6

Thread: Create keys on datatables

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Create keys on datatables

    I have a datatable that I need to find information in. I can not use a primary key since the columns do have duplicate data. How can I search in the datatable for a string. Is there a way to setup indexes?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Create keys on datatables

    Found it.

    'Find a description - Gets the Last rev description
    Dim found() As DataRow
    Dim expression As String = "fpartno = '" & lcPartno & "'"
    'Dim expression As String = "fpartno = '0904-D34N'" 'For Testing
    found = datatable.Select(expression)

    If found.GetUpperBound(0) > 0 Then
    lcDescript = found(found.GetUpperBound(0)).ItemArray(2).ToString()
    End If

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Create keys on datatables

    Yeah, that's one way. Another way would be to set the RowFilter property of the default dataview, which will returns a subset DataView. Another way would be to use LINQ, which allows you to essentially write queries against the datatable. A fourth way would be to iterate through the rows checking the fields. Interestingly, that fourth option, though it takes more code, is faster than any of the others, including the Select that you used.

    There's also a fifth option that can be even faster, but ignore that one, because it is highly specialized and situational.
    My usual boring signature: Nothing

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Create keys on datatables

    Are you aware that this:
    Code:
                    If found.GetUpperBound(0) > 0 Then
                        lcDescript = found(found.GetUpperBound(0)).ItemArray(2).ToString()
                    End If
    is getting data from the second row if and only if there are at least two rows?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2016
    Posts
    155

    Re: Create keys on datatables

    Yes, I want to retrieve that data when i found what was in column 1.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Create keys on datatables

    But what if there is only one matching row? Are you not interested in that data? Maybe you aren't and that's fine, but that would be unusual so I just wanted to make sure that it wasn't a mistake. Most people would use Length rather than GetUpperBound in that particular situation to determine whether there are any rows at all rather than determine whether there are at least two rows.

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