你已經得到了他們作爲兩個單獨的列表......我要一個飛躍,讓你需要的假設以這種格式保存源數據......將它們放在兩個列表中,您目前對它們的位置有依賴性。因此,使用可分類的集合將它們配對起來
例如,
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
如果'Price'包含'Integer'值,那麼爲什麼要將'Price(x)'放到'String'數組中?如果它們實際上是「整數」,則將值添加爲「整數」而不是「字符串」。至於排序,您只需單擊列標題或調用網格的「排序」方法。 – jmcilhinney 2014-12-02 00:06:13
示例?我不知道我該怎麼做。 – sandor 2014-12-02 01:14:08
你打的那個'Add'方法的參數類型是什麼?你看了?如果不是,爲什麼不呢?它實際上是Object()。如果該方法需要一個'Object'數組,它可以包含任何類型的對象,那麼爲什麼要傳遞一個'String'數組,它只能包含'String'對象? – jmcilhinney 2014-12-02 02:42:28