禁止在DataGridView中排序一行 [英] Disable sorting of one row in DataGridView

查看:93
本文介绍了禁止在DataGridView中排序一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的VB.NET-GUI上显示一个DataGridView。在这个表格中,最后一行是一个Total-Row,它显示一个平均值。



在列之后排序实际上是可能的,但是我想修复总行,因此它保持最后一行。这是可能的还是某种方式的标准行为?

解决方案

似乎有一个像 TotalRow 存在



可以通过使用自定义排序功能来确定。



首先,确保所有您的列具有 SortMode 设置为自动



您需要以某种方式识别您的 TotalRow ,因此您可以将其标签属性设置为字符串平均

  row.Tag =Average

然后添加以下代码:

  Private Sub DataGridView1_SortCompare(sender As Object, $ b $ as As DataGridViewSortCompareEventArgs)处理DataGridView1.SortCompare 

如果(DataGridView1.Rows(e.RowIndex1).Tag =Average)然后
e.SortResult = If(DataGridView1.SortOrder = SortOrder.Ascending,1,-1)
ElseIf(DataGridView1.Rows(e.RowIndex2).Tag =Averag e)然后
e.SortResult = If(DataGridView1.SortOrder = SortOrder.Ascending,-1,1)
Else
e.SortResult = System.String.Compare(e.CellValue1。 ToString(),
e.CellValue2.ToString())
如果

e.Handled = True

End Sub

这将会诀窍!



编辑:



由于您提到了一个平均值值,我假设您要按照数值对元素进行排序,而不是ASCII-



如果您确定DataGridView只包含数字,则可以使用大于的运算符而不是 String.Compare 在最后一个分支:

  e.SortResult = If e.CellValue1> e.CellValue2,1,-1)

这样,您的数字将被正确排序( 10,20,100,200 而不是10,100,20,200 )



如果需要比较字符串数字,我会寻找一个自然字符串比较算法


I'm showing a DataGridView on my VB.NET-GUI. In this table, the last row is a Total-Row which shows an average value.

Sorting after columns must actually be possible, but I'd like to fix the Total-Row somehow so it stays the last row. Is this possible or somehow a standard behavior?

解决方案

It seems something like a TotalRow property doesn't exist.

It's possible for sure, by using a custom ordering function.

First, be sure all your columns have SortMode set to Automatic.

You need somehow to identify your TotalRow, so you can set its Tag property to string "Average":

row.Tag = "Average"

Then add this code:

Private Sub DataGridView1_SortCompare(sender As Object,
        e As DataGridViewSortCompareEventArgs) Handles DataGridView1.SortCompare

    If (DataGridView1.Rows(e.RowIndex1).Tag = "Average") Then
        e.SortResult = If(DataGridView1.SortOrder = SortOrder.Ascending, 1, -1)
    ElseIf (DataGridView1.Rows(e.RowIndex2).Tag = "Average") Then
        e.SortResult = If(DataGridView1.SortOrder = SortOrder.Ascending, -1, 1)
    Else
        e.SortResult = System.String.Compare(e.CellValue1.ToString(),
                                             e.CellValue2.ToString())
    End If

    e.Handled = True

End Sub

This will do the trick!

EDIT :

Since you talked about an average value, I assume you want to order elements by their numeric value, instead of ASCII-betically.

If you're sure that your DataGridView will only contain numbers, you can use a greater-than operator instead of the String.Compare in the last if branch:

e.SortResult = If(e.CellValue1 > e.CellValue2, 1, -1)

This way, your numbers will be ordered correctly (10, 20, 100, 200 rather than 10, 100, 20, 200).

If one needs to compare strings and numbers, I'd look for a Natural String Compare algorithm.

这篇关于禁止在DataGridView中排序一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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