4

我的 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的来帮助我吗?

4

2 回答 2

1

该列正确排序为字符串,我想您想将其排序为数字。问题是您拥有的字符串似乎结合了数字和字符。结果是对 a 进行必要的相当复杂的排序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")

因此添加了两个新列。第一个保持按首字母排序,第二个保持按 a 字符串中包含的数字排序。

于 2013-09-09T07:55:52.023 回答
1

当 DataGridView 是数据绑定时,不能使用它的排序,必须对源数据进行排序。排序有点复杂,所以我需要两个帮助列。

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

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

现在我们将所有 comumns 设置为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 ... 不同方式排序的列。其他列按网格默认排序,或者可以创建另一个自定义排序。

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

于 2013-09-10T08:47:46.577 回答