Results 1 to 12 of 12

Thread: DataView RowFilter - How to select distinct

  1. #1

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681

    DataView RowFilter - How to select distinct

    Is it possible to select distinct in the rowfilter method?

    I have something like:
    VB Code:
    1. 'Create a data view so we can get each items name
    2.         Dim dvwItemNames As New DataView(dtlItems)
    3.         dvwItemNames.RowFilter() = "SELECT DISTINCT Name"

    This gives me a syntax error exception. I tried just "Distinct Name" but this had the same result. "Name" is the column name.

    Any ideas? I would rather do it this way then looping through the entire table and putting the name in an array list or something.

    Thanks.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You are getting a syntax error because of the parentesis on rowfilter.

    Use this dvwItemNames.RowFilter = "SELECT DISTINCT Name"

  3. #3

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    That didn't do it. I get "Syntax Error: Missing operand after 'DISTINCT' operator".

  4. #4
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You are missing the table name from the Distinct statement. You might have to make another trip to the DB to get the Distinct names.

  5. #5

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    That's what I was trying to avoid, as I already have all of the data in the datatable.

  6. #6
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Why not just get the distinct names on the first call?

  7. #7

    Thread Starter
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    I need all the records, and I also need to get at distinct column x. I didn't want to do 2 queries if I already had the data already. What I am doing now is just looping through the returned results and adding new values for column x into an array (of course I have to loop through the array to make sure it isn't already added). I don't think this is the most efficient way, but it avoids another hit to the database and another datatable in the dataset (which actually might be a better solution).

  8. #8
    New Member
    Join Date
    Feb 2006
    Posts
    1

    Re: DataView RowFilter - How to select distinct

    Hi,

    one thing to mention about the dataview, it doesn't support selecting distinct values, that's why you get syntax error, the only thing it supports is selecting rows based on a certain criteria, like "id = 334" or "name like 'MA%'"

    if you want to select the distinct values, you must make a customized function that returns a new table with distinct values only, then you make the dataview on the new table.

    You can find full converage for this topic in the following link

    http://www.dotnet247.com/247referenc...m/?kbid=325684

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataView RowFilter - How to select distinct

    The RowFilter property is equivalent to a WHERE clause from an SQL query, just like the Sort property is equivalent to an ORDER BY clause. There's no DISTINCT in a WHERE clause, only conditions. Also, RowFilter does not support the entire SQL specification. See the help/MSDN topic for DataColumn.Expression for details on what is supported.
    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

  10. #10
    New Member
    Join Date
    Feb 2010
    Posts
    1

    Re: DataView RowFilter - How to select distinct

    use

    .defaultview.totable(distinct as boolean, paramarray()) version to get distinct rows from table

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: DataView RowFilter - How to select distinct

    Quote Originally Posted by saurintravadi View Post
    use

    .defaultview.totable(distinct as boolean, paramarray()) version to get distinct rows from table
    Very cool
    I've never thought of doing it this route before.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12
    New Member
    Join Date
    Dec 2008
    Location
    Billings, MT
    Posts
    2

    Re: DataView RowFilter - How to select distinct

    Quote Originally Posted by saurintravadi View Post
    use

    .defaultview.totable(distinct as boolean, paramarray()) version to get distinct rows from table
    How do you use this code snippet? Where do you insert this?

    Thanks,
    Damon

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