首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGridViewRow数值阶

DataGridViewRow数值阶
EN

Stack Overflow用户
提问于 2014-12-01 23:36:15
回答 1查看 151关注 0票数 0

如何对DataGridViewRow进行排序以进行数字排序?

我的代码:

代码语言:javascript
复制
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

价格行示例:

代码语言:javascript
复制
1345
1533
4555
6744
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-02 12:08:57

你把它们分成两张单子.我要跳一跳,假设你需要把源数据保持在那种格式.将它们放在两个列表中,您目前对它们的位置有依赖关系。因此,使用可排序集合将它们配对。

例如:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27239378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档