2

我需要能够DataGridViewImageColumn通过单击它的标题来对它进行排序。我在设计器中将排序模式设置为自动,但是当我单击标题时没有任何反应。

这些列是否可排序?

如果是这样,我需要做什么?

谢谢!

4

2 回答 2

6

我做了一个示例,展示了如何使用自定义比较器类对 dataGridView 进行排序。正如@Andrew Morton 发布的那样,网上有一些资源,这可能会有所帮助

  1. 对 dataGridView 图像列进行排序
  2. 排序 DGV 图像列
  3. DataGridView.Sort 方法
  4. ColumHeaderCell.SortingGlyphDirection

我个人研究了 1./3。和 4. 将它们全部结合起来,我们就到了。我用两张图片(100x100 和 200x200px)对其进行了测试,并按宽度排序。当然,您可以将其更改为您想要的任何内容。

首先我创建了一个表格frmImateColumnSort.vb

Dim colName As New DataGridViewTextBoxColumn
Dim colImage As New DataGridViewImageColumn

Private Sub frmImageColumnSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' create columns (or create them with designer)
    colName.Name = "colName"
    colName.HeaderText = "Name"
    DataGridView1.Columns.Add(colName)

    colImage.Name = "colImage"
    colImage.HeaderText = "Image"
    ' we are going to set sortMode in source 
    ' so we can show a sortGlyph
    colImage.SortMode = DataGridViewColumnSortMode.Programmatic
    DataGridView1.Columns.Add(colImage)

    ' add some demo data
    DataGridView1.Rows.Add("imageA", Image.FromFile("imageA.jpg"))
    DataGridView1.Rows.Add("imageB", Image.FromFile("imageB.jpg"))
End Sub
Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
    ' in case imageColumnHeader is clicked
    If DataGridView1.Columns(e.ColumnIndex).Name = colImage.Name Then
        ' show correct sortingGlyph
        Dim currentSortOrder = DataGridView1.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection
        If currentSortOrder = SortOrder.None OrElse currentSortOrder = SortOrder.Descending Then
            currentSortOrder = SortOrder.Ascending
        Else
            currentSortOrder = SortOrder.Descending
        End If
        DataGridView1.Columns(e.ColumnIndex).HeaderCell.SortGlyphDirection = currentSortOrder
        ' use a custom comparerClass to provide sorting abilities for images
        DataGridView1.Sort(New ImageComparer(currentSortOrder))
    End If
End Sub

之后我创建了一个ImageComparer.vb类并实现了IComparer.

Public Class ImageComparer
  Implements System.Collections.IComparer

  Private sortOrderModifier As Integer = 1

  Public Sub New(ByVal sortOrder As SortOrder)
    If sortOrder = sortOrder.Descending Then
        sortOrderModifier = -1
    ElseIf sortOrder = sortOrder.Ascending Then
        sortOrderModifier = 1
    End If
  End Sub

  Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
    Implements System.Collections.IComparer.Compare

    Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
    Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)

    ' sort according to eg. Width by accessing column 1 - containing the image
    ' you may access any cell here. You might even sort imageColumn on any other colum
    Dim img1 As Image = CType(DataGridViewRow1.Cells(1).Value, Image)
    Dim img2 As Image = CType(DataGridViewRow2.Cells(1).Value, Image)
    Dim CompareResult As Integer = img1.Width.CompareTo(img2.Width)

    Return CompareResult * sortOrderModifier
  End Function
End Class

这导致以下屏幕截图

排序状态1 排序状态2

如果您正在使用绑定到 dataGridView 的数据源,则必须对数据源进行排序!例如。对数据表图像列进行排序 q&a

hth

于 2013-01-22T20:59:32.910 回答
1

在这里提问之前,您应该做一些研究(例如在 google 上搜索)。

你会发现像http://www.vbforums.com/showthread.php?559639-RESOLVED-Sorting-a-DataGridView-image-column这样的东西。

于 2013-01-22T19:26:28.543 回答