排序字母数字DataView列

时间:2013-09-06 13:15:37

标签: vb.net sorting datagridview datatable alphanumeric

我的DataGridView的 DataSource 绑定到 DataView 。 DataView等于我的dtBills DataTable 。像这样:

Dim View As New DataView
View.Table = DataSet1.Tables("dtBills")
dgvBills.DataSource = View

我在这个DataGridView中有多个列。一个特别是字符串和整数作为信息。当我单击DataGridView列标题对列进行排序时,它将排序为字符串,如左侧的列:

'Curr Col  >>>   ' Wanted Result
10001      >>>   10001
100012     >>>   11000
11000      >>>   12000
110049     >>>   100012
12000      >>>   110049
E-1234     >>>   E-1234
T-12345    >>>   T-1235
T-1235     >>>   T-12345

按常规方式按下列标题时,如何对绑定的DataGridView列进行排序?我应该使用DataView来帮助我吗?

2 个答案:

答案 0 :(得分:1)

列正确排序为字符串,我想您要将其排序为数字。问题是你的字符串似乎结合了数字和字符。结果是DataView的必要相当复杂的排序。

Dim tbl As New DataTable("dtBills")
Dim DataSet1 As New DataSet
DataSet1.Tables.Add(tbl)
tbl.Columns.Add(New DataColumn("MyCol", GetType(String)))
Dim vals As String() = {"10001", "100012", "11000", "110049", "12000", "E-1234", "T-12345", "T-1235"}
For qq = 0 To vals.Length - 1
    Dim row As DataRow
    row = tbl.NewRow
    row(0) = vals(qq)
    tbl.Rows.Add(vals(qq))
Next
tbl = DataSet1.Tables("dtBills")
tbl.Columns.Add(New DataColumn("Scol2", GetType(Integer)) With {.Expression = "Convert(iif (substring(MyCol,1,2) like '*-',substring(MyCol,3,len(MyCol)-1),MyCol), 'System.Int32')"})
tbl.Columns.Add(New DataColumn("Scol1", GetType(String)) With {.Expression = "iif (substring(MyCol,1,2) like '*-',substring(MyCol,1,2),'')"})
Dim View As New DataView(tbl)
View.Sort = "Scol1,Scol2"
View.Table = DataSet1.Tables("dtBills")

因此添加了两个新列。第一个维护按第一个字母排序第二个,以便按字符串中包含的数字排序。

答案 1 :(得分:1)

当DataGridView是数据绑定时,无法使用其排序,因此需要对源数据进行排序。排序有点复杂,所以我需要两个辅助列。

dgvBills.AutoGenerateColumns = False
tbl.Columns.Add(New DataColumn("Scol1", GetType(String)))
tbl.Columns.Add(New DataColumn("Scol2", GetType(Integer)))

第一个将包含一个前导字母(或空字符串)。第二个只包含字符串中包含的数字。我们将按Scol1, Scol2排序。

现在我们将所有列设置为Programatic模式(DataGridViewColumnSortMode Enumeration

For Each column As DataGridViewColumn In dgvBills.Columns
    column.SortMode = DataGridViewColumnSortMode.Programmatic
Next

自定义排序是在ColumnHeaderMouseClickDataGridView.Sort Method (IComparer))的处理程序中实现的。我们将使用基础视图的排序而不是网格排序。

Private Sub dgvBills_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvBills.ColumnHeaderMouseClick
    Dim newColumn As DataGridViewColumn = dgvBills.Columns(e.ColumnIndex)

    Dim direction As ListSortDirection
    Dim Modifier As String = ""

    If newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending Then
        direction = ListSortDirection.Descending
        Modifier = " desc"
    Else
        direction = ListSortDirection.Ascending
    End If

    Dim View As DataView = dgvBills.DataSource

    If {"JobNumber", "JobNumber1"}.Contains(dgvBills.Columns(e.ColumnIndex).Name) Then
        View.Table.Columns("Scol2").Expression = String.Format("Convert(iif (substring({0},1,2) like '*-',substring({0},3,len({0})-1),{0}), 'System.Int32')", dgvBills.Columns(e.ColumnIndex).Name)
        View.Table.Columns("Scol1").Expression = String.Format("iif (substring({0},1,2) like '*-',substring({0},1,2),'')", dgvBills.Columns(e.ColumnIndex).Name)
        View.Sort = String.Format("Scol1 {0},Scol2 {0}", Modifier)
    Else
        dgvBills.Sort(newColumn, direction)
    End If

    If direction = ListSortDirection.Ascending Then
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
    Else
        newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
    End If

End Sub

{"JobNumber", "JobNumber1"}.Contains ...中可以设置不同排序的列。其他列按照Grid默认排序,或者可以创建另一个自定义排序。

注意:我有完整的工作示例,但我希望片段足够好。