我可以使用DataTable.Select()方法来进行简单的分页? [英] Can i use DataTable.Select() method to make simple paging?

查看:159
本文介绍了我可以使用DataTable.Select()方法来进行简单的分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我赶紧查看此MSDN文章使用datacolumn.expression ,但都一无所获。它看起来像选择表情语法不支持分页...

I quickly view this MSDN article using datacolumn.expression, but have found nothing. It looks like 'select expression' syntax doesn't support paging...

推荐答案

寻呼是如何<机制code> SELECT 中的数据。请看下面的测试数据:

Paging is a mechanism of how you SELECT the data. Consider the following test data:

ID         Name                 Date
1          Bob                  1/1/2013
2          Bill                 1/3/2013
3          Andy                 2/1/2013
...

如果我想页面的数据,我不关心它是如何排序,我可以通过 ID ,所以我可以做这样的事情:

if I wanted to page that data, and I didn't care how it was sorted, I could do it by ID so I could do something like this:

var rows = table.Select().Take(10);
_id = (int)rows.Last()["ID"];

和这会给我的第一个十行(页) 。请注意,我还保存最后 ID 到一个名为假定类变量 _id 。你如何存储,或者只是以后恢复,即 ID 完全取决于你的程序。现在,如果我需要下一个页面,我可以做这样的事情:

and that would give me the first ten rows (i.e. a page). Note I'm also storing the last ID into an assumed class variable named _id. How you store, or just recover later, that ID depends solely on your program. Now, if I need the next page, I could do something like this:

var rows = table.Select("ID > " + _id).Take(10);
_id = (int)rows.Last()["ID"];

和这会给我的下一个页面。但如果我想排序名称数据?这改变了游戏规则一点点。 。请看下面的代码:

and that would give me the next page. But what if I wanted to sort that data by Name? That changes the game a little. Consider the following code:

var rows = table.Select("", "Name").Take(10);
_name = rows.Last()["Name"] as string;

这会给我的数据的第一页,由名称。但是请注意,我存储在假定类变量 _name 为后来的名称的值。现在,当我想下一个页面,我需要做这样的事情:

that will give me the first page of data, sorted by Name. But notice I'm storing the value of Name in the assumed class variable _name for later. Now when I want the next page I need to do something like this:

var rows = table.Select("Name > '" + _name + "'", "Name").Take(10);
_name = rows.Last()["Name"] as string;

这将让我的下一个页面,但名称排序是非常重要的,当它涉及到寻呼

that will get me the next page, but still ordered by Name. Sorting is very important when it comes to paging.

注意:代码是不是很简单,因为我已经提出。你不能只是取(10),因为可能没有 10 服用。此外,您可能不能够只是商品最后 ID 名称,你可能必须从某个地方每一次恢复。最后,你可能需要支持多排序,它是由多个列排序,所以记住这一点也提供排序时。

NOTE: the code isn't quite as simple as I've put forward. You can't just Take(10) because there may not be 10 to take. Further, you may not be able to just store the last ID or Name, you may have to recover it from somewhere every time. Finally, you may need to support multi-sort, where it's sorted by more than one column, so keep that in mind too when providing the sort.

< STRONG>最后请注意:的这是更好的利用原始的SQL 作为页面比上述代码 - 我只提供的代码,因为你问有关数据表>选择方法。这将是更好的简单构造正确的SQL语句,将其发送到服务器,然后只显示结果。

FINAL NOTE: it is much better to leverage raw SQL for paging than the aforementioned code - I only provided that code because you asked about the Select method on the DataTable. It would be much better to simply construct the correct SQL statement, send it to the server, and then just display the results.

这篇关于我可以使用DataTable.Select()方法来进行简单的分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆