简体   繁体   English

如何排序给定列和方向的数据表?

[英]How do you Sort a DataTable given column and direction?

I need to resort, in memory, a DataTable based on a column and direction that are coming from a GridView. 我需要在内存中使用基于来自GridView的列和方向的DataTable。 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. 我想我可以使用Select语句,但不确定如何。 I can't click on Comments because of this browser but you can show me an in-place or new DataTable solution, either one. 由于使用了这种浏览器,我无法单击“注释”,但是您可以向我展示就地或新的DataTable解决方案。 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;
   }

I assume "direction" is "ASC" or "DESC" and dt contains a column named "colName" 我假设“ direction”是“ ASC”或“ DESC”,并且dt包含名为“ colName”的列

public static DataTable resort(DataTable dt, string colName, string direction)
{
    DataTable dtOut = null;
    dt.DefaultView.Sort = colName + " " + direction;
    dtOut = dt.DefaultView.ToTable();
    return dtOut;
}

OR without creating dtOut 或不创建dtOut

public static DataTable resort(DataTable dt, string colName, string direction)
{
    dt.DefaultView.Sort = colName + " " + direction;
    dt = dt.DefaultView.ToTable();
    return dt;
}

If you've only got one DataView, you can sort using that instead: 如果只有一个DataView,则可以使用该视图进行排序:

table.DefaultView.Sort = "columnName asc";

Haven't tried it, but I guess you can do this with any number of DataViews, as long as you reference the right one. 还没有尝试过,但是我想只要您引用正确的DataView,就可以使用任意数量的DataView进行此操作。

Actually got the same problem. 其实有同样的问题。 For me worked this easy way: 对我来说,这种简单的工作方式是:

Adding the data to a Datatable and sort it: 将数据添加到数据Datatable并对其进行排序:

dt.DefaultView.Sort = "columnname";
dt = dt.DefaultView.ToTable();

DataTables have an overloaded Select method that you can you to do this. DataTables具有重载的Select方法,您可以执行此操作。 See here: http://msdn.microsoft.com/en-us/library/way3dy9w.aspx 看到这里: 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. 但是Select调用的返回值不是DataTable,而是RowData对象的数组。 If you want to return a DataTable from your function you will have to build it from scratch based on that data array. 如果要从函数返回DataTable,则必须基于该数据数组从头开始构建它。 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/ 这是一篇文章,介绍并解决了这两个问题: http : //social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/157a4a0f-1324-4301-9725-3def95de2bf2/

Create a DataView . 创建一个DataView You cannot sort a DataTable directly, but you can create a DataView from the DataTable and sort that. 您不能直接对DataTable排序,但是可以从DataTable创建DataView并对其进行排序。

Creating: http://msdn.microsoft.com/en-us/library/hy5b8exc.aspx 创建: http : //msdn.microsoft.com/en-us/library/hy5b8exc.aspx

Sorting: http://msdn.microsoft.com/en-us/library/13wb36xf.aspx 排序: http : //msdn.microsoft.com/en-us/library/13wb36xf.aspx

The following code example creates a view that shows all the products where the number of units in stock is less than or equal to the reorder level, sorted first by supplier ID and then by product name. 下面的代码示例创建一个视图,该视图显示所有库存数量小于或等于再订购水平的产品,首先按供应商ID,然后按产品名称排序。

DataView prodView = new DataView(prodDS.Tables["Products"], "UnitsInStock <= ReorderLevel", "SupplierID, ProductName", DataViewRowState.CurrentRows);

In case you want to sort in more than one direction 如果您想在多个方向上排序

  public static void sortOutputTable(ref DataTable output)
        {
            DataView dv = output.DefaultView;
            dv.Sort = "specialCode ASC, otherCode DESC";
            DataTable sortedDT = dv.ToTable();
            output = sortedDT;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM