gpt4 book ai didi

.net - DataGridView 排序不起作用

转载 作者:行者123 更新时间:2023-12-02 17:34:10 26 4
gpt4 key购买 nike

Visual Studio 2010 VB.NET

我有一个 DataGridView.DataSource 设置为自定义对象的集合。这些列使用自定义对象的只读属性进行显示,此对话框仅用于显示。这些属性都返回 String 对象。我将除其中两列之外的所有列设置为自动排序模式(未设置的列是按钮或复选框)。但它没有排序。我用谷歌搜索了一下,大多数人都使用 sql 或绑定(bind)源,但我使用的是一个简单的 vb 集合。有人说我应该实现 IComparable,但 String 不是已经是 IComparable 了吗?

如有任何帮助,我们将不胜感激?

谢谢

博杰

根据请求,这里有一些代码片段。

此方法详细定义了我在设计师。

列名称称为 pSelected 或 pCustomer,并与属性与列定义关联的名称相同。


Protected Sub UpdateDGVUS()
If Not USColumnsInitted Then
USColumnsInitted = True

dgvUS.AutoGenerateColumns = False
dgvUS.Columns.Clear()

Dim iIdx As Integer

iIdx = 0

dgvUS.Columns.Insert(iIdx, Me.pSelected)
dgvUS.Columns("pSelected").DisplayIndex = iIdx
dgvUS.Columns("pSelected").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pCustomer)
dgvUS.Columns("pCustomer").DisplayIndex = iIdx
dgvUS.Columns("pCustomer").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pDetails)
dgvUS.Columns("pDetails").DisplayIndex = iIdx
dgvUS.Columns("pDetails").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pSelectCustomerInvoice)
dgvUS.Columns("pSelectCustomerInvoice").DisplayIndex = iIdx
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pDate)
dgvUS.Columns("pDate").DisplayIndex = iIdx
dgvUS.Columns("pDate").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pAmount)
dgvUS.Columns("pAmount").DisplayIndex = iIdx
dgvUS.Columns("pAmount").DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
dgvUS.Columns("pAmount").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pName)
dgvUS.Columns("pName").DisplayIndex = iIdx
dgvUS.Columns("pName").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pPayment)
dgvUS.Columns("pPayment").DisplayIndex = iIdx
dgvUS.Columns("pPayment").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pCompany)
dgvUS.Columns("pCompany").DisplayIndex = iIdx
dgvUS.Columns("pCompany").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pType)
dgvUS.Columns("pType").DisplayIndex = iIdx
dgvUS.Columns("pType").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dgvUS.Columns.Insert(iIdx, Me.pDescription)
dgvUS.Columns("pDescription").DisplayIndex = iIdx
dgvUS.Columns("pDescription").SortMode = DataGridViewColumnSortMode.Automatic
iIdx = iIdx + 1

dataUpdatedUS()
End If
End Sub

这是集合中自定义对象的片段


Public Class ItemXact01
Public Property FirstName As String
Public Property LastName As String
Public Property Company As String
Public Property Type As String
Public Property Description As String
Public Property RefNumber As String
Public Property DownloadID As String
Public Property Selected As Boolean
Public Property RequestID As Integer

...

Public ReadOnly Property pCompany As String
Get
pCompany = Company
End Get
End Property

Public ReadOnly Property pType As String
Get
pType = Type
End Get
End Property

Public ReadOnly Property pDescription As String
Get
pDescription = Description
End Get
End Property

Public ReadOnly Property pSelected As Boolean
Get
pSelected = Selected
End Get
End Property

...

数据通过此代码放置到位


Private Sub dataUpdateDGV(ByRef dgv As DataGridView, ByRef myCollection As Collection)
myMain.Log("dataUpdatedDGV: 001 :" & dgv.RowCount & ":" & myCollection.Count & ":")

' for some reason the not equal to does not show in the next line

If dgv.RowCount myCollection.Count Then
myMain.Log("dataUpdatedDGV: 002")

dgv.DataSource = Nothing
If myCollection.Count > 0 Then
myMain.Log("dataUpdatedDGV: 003")

dgv.DataSource = myCollection
End If
End If

myMain.Log("dataUpdatedDGV: 004")

dgv.Invalidate()
dgv.Update()
dgv.Refresh()

myMain.Log("dataUpdatedDGV: OUT")
End Sub

最佳答案

这里的关键是 DataGridView 不负责排序;而是负责排序。底层数据源(即 DataSource 的 .DataSource)是。

你可以实现像这样的SortableBindingList(对于Windows.Forms) http://www.martinwilley.com/net/code/forms/sortablebindinglist.html

关于.net - DataGridView 排序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4137635/

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