gpt4 book ai didi

vb.net - 排序 DataGridView 图像列

转载 作者:行者123 更新时间:2023-12-02 07:37:45 25 4
gpt4 key购买 nike

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

这些列可以排序吗?

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

谢谢!

最佳答案

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

  1. > Sorting a dataGridView image Column
  2. > Sorting DGV image Column
  3. > DataGridView.Sort Method
  4. > ColumHeaderCell.SortingGlyphDirection

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

首先我创建了一个表单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

如果您正在使用绑定(bind)到 dataGridView 的数据源,则必须对数据源进行排序!例如。 Sorting a dataTable image column q&a

第一个

关于vb.net - 排序 DataGridView 图像列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14465439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com