对DataGridView图像列进行排序

时间:2013-01-22 18:23:46

标签: vb.net datagridview

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

这些列是否可以排序?

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

谢谢!

2 个答案:

答案 0 :(得分:6)

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

  1. Sorting a dataGridView image Column
  2. Sorting DGV image Column
  3. DataGridView.Sort Method
  4. ColumHeaderCell.SortingGlyphDirection
  5. 我个人看了1./3。并且把它们组合在一起,我们在这里。我用两个图片(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
    

    这导致以下屏幕截图

    sortState1 sortState2

    如果您正在使用绑定到dataGridView的dataSource,则必须对DataSource进行排序!例如。 Sorting a dataTable image column q&a

    HTH

答案 1 :(得分:1)

你想在这里询问之前做一些研究(例如谷歌搜索)。

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