Out of the box, you can do a lot with the DataView.RowFilter property, but you can’t make use of the top clause. However, if the view contains a column with unique values, you can obtain the same results by setting the Sort property and then generating a RowFilter which uses the in condition.

Here’s a sample:

public static DataView GetLatestComments(int numComments) { DataView Comments = new DataView(GetComments()); Comments.Sort = DateCreated Desc; string filter = string.Empty; string delimiter = string.Empty; numComments = Math.Min(numComments, Comments.Count); for (int i = 0; i < numComments; i++) { filter += delimiter + Comments[i]["Id"].ToString(); delimiter = ,; } if (filter.Length > 0) { Comments.RowFilter = Id in ( + filter + ); } return Comments; }

Note how the delimited string is built.  Phil Haack provided an interesting tip on concatenating a delimited string last year.  Undoubtedly, we’ve all built delimited strings using our preferred method.  If you sift through the post comments, you will see what I mean.  Anyhow, I took a liking to the method Joe Cheng uses when he says he’s “feeling lazy and performance is not critical (which is the vast majority of the time).”

8 Comments to “DataView.RowFilter Top Rows”

  1. Jon Galloway says:

    That’s a nice solution. I’ve used a PagedDataSource before (http://www.dotnetjunkies.com/W.....33757.aspx), and it works reasonably well if you’re doing simple things with it. Another technique is to add an autoincrement column (http://preview.tinyurl.com/25yolj).

    Your solution keeps things simple, which is nice.

  2. Jon Galloway says:

    [not sure what happened - I left a longer comment yesterday that didn't go through]

    Nice indeed. I’ve previously used the PagedDataSource to do this kind of thing, and read about techniques to add an autoincrement column and use “where newcolumn

  3. Mrdec says:

    Hello, here can be found detailed description of Dataview.RowFilter syntax with many examples.

  4. Ralph says:

    I was actually looking for this on google, and this page was my first result.

    Instead of creating a DataView, I decided to go for this:
    ds.Tables.Add();
    ds.Tables[1].Columns.Add(“Title”);
    for (int i = 0; i < itemMax; i++)
    {
    DataRow row = ds.Tables[1].NewRow();
    row["Title"] = ds.Tables[0].Rows[i]["Title"];
    ds.Tables[1].Rows.Add(row);
    }

    That’s disappointing that DVs don’t have TOP…

  5. Ralph says:

    … oops, had to make a correction.

    for (int i = 0; i < MAX(itemMax, ds.Tables[0].Rows.Count); i++)

    I ran into something similar before… I kind of like the solution.

  6. Ralph says:

    Did i say MAX? I meant, Math.Min

    ARRGGH

  7. Ben says:

    Third time is a charm, eh? Thanks for the contribution, Ralph.

  8. Shirl Seats says:

    Work is a fucking scam. What kind of world is this that some people spend 60 hours a week pulling fries out of hot grease? What does a $300,000 car say to the cosmos? Please, fucking wipe us out with a giant asteroid already?, is what.