VB.NET中数据的排序问题

VB.NET中数据的排序问题,第1张

建议用 DataGridView(你用的是它吧?)内建的排序方法来排序。介绍和示例代码可以参考MSDN:http://msdn.microsoft.com/zh-cn/library/wstxtkxs(VS.80).aspx

窗体上添加3个标签,1个按钮,在按钮的单击事件里写代码,如下:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim a(,) As Integer = {{5, 6}, {1, 3}, {8, 9}, {72, 1}, {63, 4}}

Dim Temp As Integer

Dim i As Integer

Dim j As Integer

Dim x As Integer

Dim y As Integer

'显示排序前的数据

Label1.Text = "排序前数据:" &vbCrLf

For i = 0 To 4

Label1.Text = Label1.Text &a(i, 0) &" " &a(i, 1) &vbCrLf

Next

For i = 0 To 3

For j = i + 1 To 4

If a(i, 0) >a(j, 0) Then

Temp = a(i, 0)

a(i, 0) = a(j, 0)

a(j, 0) = Temp

Temp = a(i, 1)

a(i, 1) = a(j, 1)

a(j, 1) = Temp

End If

Next

Next

'显示排序前的数据

Label2.Text = "排序后数据:" &vbCrLf

For i = 0 To 4

Label2.Text = Label2.Text &a(i, 0) &" " &a(i, 1) &vbCrLf

Next

'把第3行元素赋予X,Y

x = a(2, 0)

y = a(2, 1)

'输出X,Y

Label3.Text = "X=" &x &vbCrLf &"Y=" &y

End Sub

// 数据排序(该步骤仅对默认视图进行排序)

datatable.DefaultView.Sort = "序号"

// 把数据视图更新到数据结果集 (将默认视图的已经排序的内容复制到table中)

datatable = datatable.DefaultView.ToTable()

参考资料:http://www.dearboy.cn/a/201407/472.html


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9608638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存