如何以数字方式对DataGridView列进行排序? [英] How to sort a DataGridView Column numerically?
本文介绍了如何以数字方式对DataGridView列进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有未绑定DataGridView的应用程序,第一列的内容是毫秒值。问题是它将它们分类为字符串而不是整数。
例如:
21045
2305
340980
当它应该排序为:
2305
3700
21045
340980
我需要它按行从最小(顶部)到最大(底部)的第一列[ms]的值对行进行排序。
有人可以帮忙吗?
I have an app with an unbound DataGridView and the first column's contents are millisecond values. The problem is that it sorts them as strings not integers.
Example:
21045
2305
340980
3700
when it should sort as:
2305
3700
21045
340980
I need it to sort the rows by the value of the first column[ms] from least(top) to greatest(bottom).
Can someone please help?
推荐答案
其中一个选项是使用SortCompare
事件DataGridView
如下所示
One of the options is to use theSortCompare
event ofDataGridView
as shown below
Private Sub dataGridView1_SortCompare(sender As Object, e As DataGridViewSortCompareEventArgs)
If e.Column.Index <> 0 Then
Return
End If
Try
e.SortResult = If(CInt(e.CellValue1) < CInt(e.CellValue2), -1, 1)
e.Handled = True
Catch
End Try
End Sub
我不想减损VJ Reddy的解决方案,但是如果DataGridview没有绑定到数据源,并且你以编程方式添加数据,你可以尝试写一个整数数据类型(或其他数字数据一个类型)到DataGridview单元格。然后它也正确排序。
I dont want to detract from VJ Reddy's solution, but if the DataGridview is not bound to a datasource, and are you adding the data programatically you can try writing an integer data type (or other numeric data type) to the DataGridview cells. Then it also sorts correctly.
如果DataGridview绑定到数据源,在添加列期间确保包含数据类型:
datatable.Columns.Add(Quantity,GetType(Integer))
if the DataGridview is bound to a datasource, during add column ensure to include data type:
datatable.Columns.Add("Quantity", GetType(Integer))
这篇关于如何以数字方式对DataGridView列进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文