dcsimg
Results 1 to 6 of 6

Thread: Create keys on datatables

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2016
    Posts
    51

    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
    Member
    Join Date
    Apr 2016
    Posts
    51

    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
    32,243

    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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2016
    Posts
    51

    Re: Create keys on datatables

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

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,420

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width