我正在使用VB.NET和Visual Studio 2010
我有一个带有组合框的窗体。
我使用以下
填充组合框Dim objSizes As List(Of ASME_Hub_Sizes) = ASME_Hub_Sizes.GetAllHubSizes()
If Not objSizes Is Nothing Then
With Me.cboSize
.DisplayMember = "Size"
.ValueMember = "ID"
.DataSource = objSizes
End With
End If
这很好用,但我想添加一个“选择尺寸...”选项,但我不确定如何做到这一点。
在asp.net中执行此操作似乎要容易得多,但这让我感到困惑
由于 米克
答案 0 :(得分:0)
您可以尝试将自定义objSize对象添加到ID = 0且值=“选择大小...”的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.
因此,您可以像往常一样获取列表,添加额外的虚拟记录,然后根据上面的代码对其进行排序,然后再将其指定为组合框数据源。
答案 1 :(得分:0)
只需在设置数据源属性
之前添加项目C#:cboSize.items.add(...);