CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2006
    Posts
    28

    Filter datatable using an array of values?

    I have a datatable called dt which includes a column called type
    I have a string array called typestringarray with about 15 members

    I want to create a new datatable from the rows where type is a member of typestringarray

    here's a simplified example of my problem:

    dt

    id type
    1 red
    2 blue
    3 orange
    4 green
    5 pink
    6 green

    typestringarray = {"red", "blue", "green")

    so my new table would be

    id type
    1 red
    2 blue
    4 green
    6 green

    I'm not sure how to start.

    defaultview.rowfilter doesn't look right, as my filter would have 15 or more OR clauses.

    There must be a really simple answer that I'm missing?
    I'm using .NET Framework 3.5

    I'm planning to be spontaneous tomorrow

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Filter datatable using an array of values?

    Nope, you'd need 15 SQL statements, but you could use a LOOP
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: Filter datatable using an array of values?

    Try:

    {"red", "blue", "green"}.Contains(type)

    as you itterate through dt

  4. #4
    Join Date
    Dec 2007
    Posts
    234

    Re: Filter datatable using an array of values?

    You should be able to use an IN clause... no?

    Code:
    .RowFilrer = "Field IN ('Red','Green','Blue')"
    -tg
    * I don't respond to private requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help - how to remove eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to???
    * On Error Resume Next is error ignoring, not error handling(tm). * Use Offensive Programming, not Defensive Programming.
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN
    MVP '06-'10

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

Featured