3

I know there's the RowFilter option to filter according to column value. Also there are methods to choose the top N rows. But how do I filter out and get rows from (I prefer getting it to the same DataView dv), say, position 10 to position 23?

Here's my requirement. I have a DataView dv which has 100 rows. I have a listbox with 10 items. When I choose first item in the listbox, I want first 10 rows of the dataview to be loaded (loading part is in my program, leave it to me), if I choose 2nd item in listbox then I want row11 to row20 to be loaded and so on. I can do the listbox part, but how to choose dataview values based on row number?

This is how my code looks:

            DataSet ds = new DataSet();
            DataTable dt = ds.Tables["words"];
            DataView dv = new DataView(dt);

Now how to have a dataview from dv based on row position?

Thanks.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • One simple way would be to add a column which will hold the values as the rowindex and you can then use the same in the **RowFilter** – V4Vendetta Apr 22 '11 at 04:19
  • @V4Vendetta, that may not work, or become equally complicated (then again I have to assign column name for first ten rows, then another name for next ten rows, basically a bigger hassle) considering all row of database are created on the fly during the program and need not be there in the next instant. – nawfal Apr 22 '11 at 04:23

1 Answers1

4

You can utilize the extension methods provided in Linq to get rows by position. For example:

// just setting up a table for the sample
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));

for (int i = 1; i <= 100; i++)
{
    table.Rows.Add(i);
}

// grabbing rows 11 through 20 using Linq
DataTable filtered = table.AsEnumerable().Skip(10).Take(10).CopyToDataTable();

The above works with .NET 3.5+, C# 3.0+. For something that works in older versions of C# and .NET, you can do it manually in just a little more code.

// starting by cloning 'table' (see code above)
DataTable filtered = table.Clone();

int skipRows = 10;
int selectRows = 10;

for (int index = skipRows; 
     index < skipRows + selectRows && index < table.Rows.Count; 
     index++)
{
    filtered.Rows.Add(table.Rows[index].ItemArray);
}
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • @Anthony, thanks a ton. would this Linq method work in C#2.0 (visual studio 2005) ? – nawfal Apr 22 '11 at 04:11
  • 1
    @nawful, no, it would not. I can update to show you 2.0 compliant code, give me a moment. It's basically manual work. – Anthony Pegram Apr 22 '11 at 04:13
  • @Anthony Pegram, can you explain to me ( i'm using c# 2.0 of course) do I need to include this line in my code `table.Columns.Add("ID", typeof(int));` ?? If so how does it help our logic.. Also, `for (int i = 1; i <= 100; i++) { table.Rows.Add(i); }` what are we doing with this line of code? I already have a fully filled `DataTable` table. In that case I can skip this procedure right – nawfal Apr 22 '11 at 05:28
  • @nawful, ignore all of that stuff building the `table` object. That was just to provide a fully working example in the answer. For your part, you can start with the `filtered` object. – Anthony Pegram Apr 22 '11 at 05:37
  • Yes I realized that. I'd done already. just a confirmation.. :) – nawfal Apr 22 '11 at 05:45