您可以尝试将自定义 objSize 对象添加到 ID = 0 且 value = "Select size..." 的 objSizes 集合中,因为它的 ID 为 0,它应该位于顶部(我认为)并且不会与中的任何值发生冲突您的数据库,在保存记录后,您可以验证组合框以避免将“选择大小...”对象写入数据库。我会有一些代码,看看这是否可行......
好的,我又看了一遍。您可以按照我的建议进行操作,但您必须在将列表传递给组合框之前对其进行排序。这是我的例子:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With Me.ComboBox1
.DisplayMember = "Description"
.ValueMember = "ID"
.DataSource = GetListOfObjects.returnListOFObjects
End With
End Sub
End Class
Public Class dummyObjectForCombo
Public Property ID As Integer
Public Property Description As String
Public Sub New(ByVal id As Integer,
ByVal description As String)
_ID = id
_Description = description
End Sub
End Class
Public Class GetListOfObjects
Public Shared Function returnListOFObjects() As List(Of dummyObjectForCombo)
Dim col As New List(Of dummyObjectForCombo)
Dim obj0 As New dummyObjectForCombo(-1, "Herp")
Dim obj1 As New dummyObjectForCombo(1, "Jamie")
Dim obj2 As New dummyObjectForCombo(2, "Bob")
col.Add(obj1)
col.Add(obj2)
col.Add(obj0)
'using a lambda to sort by ID as per http://stackoverflow.com/questions/3309188/c-net-how-to-sort-a-list-t-by-a-property-in-the-object
Return col.OrderBy(Function(x) x.ID).ToList
End Function
End Class
我使用 -1 而不是 0 作为最高记录。
所以你会像往常一样得到你的列表,添加额外的虚拟记录,然后按照上面的代码对其进行排序,然后再将其分配为组合框数据源。