DataGridViewRow按数字顺序排列

时间:2014-12-01 23:36:16

标签: vb.net visual-studio-2010 datagridview rows

如何将DataGridViewRow排序为数字排序?

我的代码:

Dim Name As New List(Of String)
Dim Price As New List(Of Integer)

   For x = 0 To Math.Max(Name.Count, Price.Count)
                If x < Name.Count AndAlso x < Price.Count Then
                Dim row As String() = New String() {Name(x), Price(x)}
                DataGridView1.Rows.Add(row)
            End If
        Next

价格行示例:

1345
1533
4555
6744

1 个答案:

答案 0 :(得分:0)

您已将它们作为两个单独的列表...我将进行一次飞跃,并假设您需要将源数据保持为该格式...将它们放在两个列表中即可#&# 39;目前他们的职位依赖。因此,使用可排序的集合来配对它们

e.g。

Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Data.Linq

    Dim Name As New List(Of String)
    Dim Price As New List(Of Integer)
    Dim Sortable As New List(Of KeyValuePair(Of String, Integer))

    'Your checking that you weren't going past the last pair was an odd one
    For x = 0 To Math.Min(Name.Count, Price.Count) - 1
        'Pair them up so you can sort them together not having to try to match between two lists
        Sortable.Add(New KeyValuePair(Name(x), Price(x)))
    Next

    'now you can sort it
    Sortable = (From item In Sortable Order By Convert.ToInt32(item.Key) Select item).ToList()

    'now stick it in the grid view, You might be able to base the grid view on the 
    'DataGridView1.DataSource = Sortable
    'DataGridView1.DataBind()
    'depends on how it is defined/configured, 
    'but for now let us return to your original code....
    For Each kvp As KeyValuePair(Of String, Integer) In Sortable
        Dim row As String() = New String() {kvp.Key, kvp.Value.ToString("$0.00")}
        DataGridView1.Rows.Add(row)
    Next