简体   繁体   English

我如何对数据表进行排序

[英]How do I sort a datatable

How do I sort a datatable? 如何对数据表进行排序? I need to return a datatable from a function. 我需要从函数返回一个数据表。 I have been struggling with this for hours, and the internet has a few different answers, none of which seem to work for me. 我一直在努力奋斗这几个小时,互联网有几个不同的答案,其中没有一个似乎对我有用。

Edit: I want to punch myself. 编辑:我想打自己。 Do a DataView.Sort on your table, then a DataView.ToTable() to put the sorted data into a new dataset... Example: 在表上执行DataView.Sort,然后使用DataView.ToTable()将已排序的数据放入新数据集中...示例:

Dim view As New DataView(OriginalDataSet) 'Put your original dataset into a dataview
view.Sort = "ColumnName" ' Sort your data view
Dim NewDataSet As DataTable = view.ToTable() ' Put your dataview into a new datatable

End of example 示例结束

I have a relatively simple example table below, taken from a teaching website. 我有一个相对简单的示例表,取自教学网站。 The one twist is that there are duplicate values in the row I am trying to sort on. 一个转折是在我尝试排序的行中有重复的值。

Module Module1

    Sub Main()
    ' Get a DataTable instance from helper function.
    Dim table As DataTable = GetTable()
    End Sub

    ''' <summary>
    ''' Helper function that creates new DataTable.
    ''' </summary>
    Function GetTable() As DataTable
    ' Create new DataTable instance.
    Dim table As New DataTable
    ' Create four typed columns in the DataTable.
    table.Columns.Add("Dosage", GetType(Integer))
    table.Columns.Add("Drug", GetType(String))
    table.Columns.Add("Patient", GetType(String))
    table.Columns.Add("Date", GetType(DateTime))
    ' Add five rows with those columns filled in the DataTable.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now)
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now)
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now)
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now)
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now)
table.Rows.Add(21, "Aspirin", "Janet", DateTime.Now)
    Return table
    End Function

End Module

I have tried selecting into an array, then looping through the array and putting each row into a new datatable, but the select isn't grabbing rows. 我已经尝试选择一个数组,然后循环遍历数组并将每一行放入一个新的数据表,但select不是抓取行。 Example: 例:

drarray = ds.Select("I want to select all here", "MySortColumn") drarray = ds.Select(“我想在这里选择全部”,“MySortColumn”)

I have tried various looping strategies, with inner loops, etc and can't seem to figure that out. 我已经尝试了各种循环策略,内部循环等,似乎无法弄明白。

I have tried dataTable.DefaultView.Sort = "sortExp" but I can't get that to work. 我已经尝试过dataTable.DefaultView.Sort =“sortExp”,但我无法让它工作。

So what am I missing? 那我错过了什么? I figure with the DefaultView and Select methods I'm just missing something syntactly. 我想用DefaultView和Select方法,我只是在语法上遗漏了一些东西。

So what's the best way to go, and what am I missing? 那么最好的方法是什么,我错过了什么?

你可以使用这样的东西:

Return table.Select("","Columns to sort on").CopyToDataTable

Use a DataView to create a view of your data in the DataTable. 使用DataView在DataTable中创建数据视图。 This allows you to sort, filter, etc. Here's a C# example: Datatable VS dataview 这允许您进行排序,过滤等。这是一个C#示例: Datatable VS dataview

This may help you sortExp can be field on which based the sort should be performed filterExp that should evaluate to true or false. 这可能有助于sortExp可以是应该执行排序的字段filterExp应该评估为true或false。 assuming the following fields 假设以下字段

Dim filterExp As String = "Patient<> ''"
Dim sortExp As String = "Date "
dt_item.Select(filterExp, sortExp, DataViewRowState.CurrentRows)

The above code shows how to filter and sort the data table dt_item. 上面的代码显示了如何过滤和排序数据表dt_item。 The filter expression selects Date whose Patient is not NULL. 过滤器表达式选择Patient不为NULL的Date。 The sort expression causes the results to be sorted by the Date column 排序表达式导致结果按Date列排序

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

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