在datagridview VB net 2010中快速简短 [英] Quick short in datagridview VB net 2010

查看:96
本文介绍了在datagridview VB net 2010中快速简短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我,在datagridview中实现快速排序算法吗?





我想对id栏目进行排序



id姓名地址

================ =======================

11 joni kl

290 lkvan js

829 merry le

91预付款sw





并通过按钮执行











谢谢



我尝试了什么:



我使用这样的代码,但不是排序值11和91



Can anyone help me, to implement a quick sort algorithm in datagridview?


I want to sort section "id" column

id name address
=======================================
11 joni kl
290 lkvan js
829 merry le
91 laun sw


and execute it via the button





thanks

What I have tried:

I use code like this, but not sort value 11 and 91

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

       DataGridView1.Sort(DataGridView1.Columns(0),
       System.ComponentModel.ListSortDirection.Ascending)

End Sub

推荐答案

如果您的DataGridView的 DataSource属性 [ ^ ]与 DataTable 绑定,您可以通过这种方式对数据进行排序:



If your DataGridView's DataSource property[^] is bound with DataTable, you can sort data this way:

Dim dt As DataTable = DirectCast(DataGridView1.DataSource, DataTable)
'when [id] field is type of integer
'Dim result = dt.AsEnumerable().OrderBy(Function(x) x.Field(Of Integer)("id"))
	
'when [id] field is type of string
Dim result = dt.AsEnumerable().OrderBy(Function(x) Convert.ToInt32(x.Field(Of String)("id")))

dt = result.CopyToDataTable()
DataGridView1.DataSource = dt





详情请见:

排序和过滤数据| Microsoft Docs [ ^ ]

如何:将数据绑定到Windows窗体DataGridView控件Microsoft Docs [ ^ ]

DirectCast Operator(Visual Basic)| Microsoft Docs [ ^ ]



如果你的 DataGridView 没有绑定任何数据来源,你可以通过Linq查询排序 DataGridView 行。



For further details, please see:
Sorting and Filtering Data | Microsoft Docs[^]
How to: Bind Data to the Windows Forms DataGridView Control | Microsoft Docs[^]
DirectCast Operator (Visual Basic) | Microsoft Docs[^]

In case when your DataGridView is not binded with any data source, you can sort DataGridView rows via Linq query.

Dim sortedDGVRows = DataGridView1.Rows. _
    OfType(Of DataGridViewRow)(). _
    OrderBy(Function(r) r.Cells("id"))
DataGridView1.Rows = sortedDgvRows


这篇关于在datagridview VB net 2010中快速简短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆