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

发布于 2024-10-17 18:45:55 字数 639 浏览 2 评论 0原文

我需要在内存中使用基于来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

荒路情人 2024-10-24 18:45:55

我假设“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;
}

,或者不创建 dtOut

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

I assume "direction" is "ASC" or "DESC" and dt contains a column named "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

public static DataTable resort(DataTable dt, string colName, string direction)
{
    dt.DefaultView.Sort = colName + " " + direction;
    dt = dt.DefaultView.ToTable();
    return dt;
}
丿*梦醉红颜 2024-10-24 18:45:55

如果您只有一个 DataView,则可以使用它进行排序:

table.DefaultView.Sort = "columnName asc";

还没有尝试过,但我想您可以对任意数量的 DataView 执行此操作,只要您引用正确的 DataView。

If you've only got one DataView, you can sort using that instead:

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.

巴黎夜雨 2024-10-24 18:45:55

其实也遇到了同样的问题。对我来说,工作方式很简单:

将数据添加到 Datatable 并对其进行排序:

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

Actually got the same problem. For me worked this easy way:

Adding the data to a Datatable and sort it:

dt.DefaultView.Sort = "columnname";
dt = dt.DefaultView.ToTable();
凉城已无爱 2024-10-24 18:45:55

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/

ヤ经典坏疍 2024-10-24 18:45:55

如果您想按多个方向排序

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

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;
        }
雨后咖啡店 2024-10-24 18:45:55

创建一个数据视图。您不能直接对 DataTable 进行排序,但您可以从 DataTable 创建一个 DataView 并对其进行排序。

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

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

以下代码示例创建一个显示所有产品的视图
库存单位数量小于或等于
再订购级别,首先按供应商 ID 排序,然后按产品名称排序。

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文