如何对给定列和方向的数据表进行排序?
我需要在内存中使用基于来自 GridView 的列和方向的 DataTable。该函数需要如下所示:
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
....
}
我需要帮助填写此函数。我想我可以使用 Select 语句,但我不知道如何使用。由于此浏览器,我无法单击“评论”,但您可以向我展示就地或新的 DataTable 解决方案,无论是其中之一。对于那些向我展示指针的人,我需要一个类似于原型的编码函数。
怎么样:
// ds.Tables[0].DefaultView.Sort="au_fname DESC";
public static void Resort(ref DataTable dt, string colName, string direction)
{
string sortExpression = string.Format("{0} {1}", colName, direction);
dt.DefaultView.Sort = sortExpression;
}
I need to resort, in memory, a DataTable based on a column and direction that are coming from a GridView. The function needs to look like this:
public static DataTable resort(DataTable dt, string colName, string direction)
{
DataTable dtOut = null;
....
}
I need help filling in this function. I think I can use a Select statement but I am not sure how. I can't click on Comments because of this browser but you can show me an in-place or new DataTable solution, either one. For the people showing me pointers, please, I need a coded function similar to the one prototyped.
How about:
// ds.Tables[0].DefaultView.Sort="au_fname DESC";
public static void Resort(ref DataTable dt, string colName, string direction)
{
string sortExpression = string.Format("{0} {1}", colName, direction);
dt.DefaultView.Sort = sortExpression;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我假设“direction”是“ASC”或“DESC”,并且 dt 包含名为“colName”的列
,或者不创建 dtOut
I assume "direction" is "ASC" or "DESC" and dt contains a column named "colName"
OR without creating dtOut
如果您只有一个 DataView,则可以使用它进行排序:
还没有尝试过,但我想您可以对任意数量的 DataView 执行此操作,只要您引用正确的 DataView。
If you've only got one DataView, you can sort using that instead:
Haven't tried it, but I guess you can do this with any number of DataViews, as long as you reference the right one.
其实也遇到了同样的问题。对我来说,工作方式很简单:
将数据添加到
Datatable
并对其进行排序:Actually got the same problem. For me worked this easy way:
Adding the data to a
Datatable
and sort it:DataTables 有一个重载的 Select 方法,您可以使用它来执行此操作。请参阅此处:http://msdn.microsoft.com/en-us/library/ way3dy9w.aspx
但是 Select 调用的返回值不是 DataTable 而是 RowData 对象的数组。如果您想从函数返回 DataTable,则必须根据该数据数组从头开始构建它。以下帖子解决了这两个问题并提供了示例:http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/157a4a0f-1324-4301-9725-3def95de2bf2/
DataTables have an overloaded Select method that you can you to do this. See here: http://msdn.microsoft.com/en-us/library/way3dy9w.aspx
But the return val of the Select call is not a DataTable but an array of RowData objects. If you want to return a DataTable from your function you will have to build it from scratch based on that data array. Here is a post that addresses and provides a sample for both issues: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/157a4a0f-1324-4301-9725-3def95de2bf2/
如果您想按多个方向排序
In case you want to sort in more than one direction
创建一个数据视图。您不能直接对 DataTable 进行排序,但您可以从 DataTable 创建一个 DataView 并对其进行排序。
创建: http://msdn.microsoft.com/en-us/library/hy5b8exc .aspx
排序:http://msdn.microsoft.com/en -us/library/13wb36xf.aspx
DataView prodView = new DataView(prodDS.Tables["产品"],
“库存数量 <= 重新订购级别”,
“供应商ID,产品名称”,
DataViewRowState.CurrentRows);
Create a DataView. You cannot sort a DataTable directly, but you can create a DataView from the DataTable and sort that.
Creating: http://msdn.microsoft.com/en-us/library/hy5b8exc.aspx
Sorting: http://msdn.microsoft.com/en-us/library/13wb36xf.aspx
DataView prodView = new DataView(prodDS.Tables["Products"],
"UnitsInStock <= ReorderLevel",
"SupplierID, ProductName",
DataViewRowState.CurrentRows);