我有一个 Datagridview,它根据用户在列表框中所做的选择来更改其内容。DGV 由 2 个组合框(国家、产品)和 1 个文本框(数量)组成。
我创建了一个由 3 个整数组合而成的类。此类用作列表类型,它是 DGV 的数据源。还有另一个包含先前列表的列表,所以我有一个数据源列表。
DGV 的数据源是一个 BindingSource,只要触发 listBox 的 SelectedIndex,它就会发生变化。
每当将新行添加到 DGV 时,就会出现我的问题:我使用调用类的构造函数的 BindingSource.AddNew,但它必须为类中的每个项目分配值。这样,每当我单击 DGV 中的任何单元格时,我都不会得到空白行。而且,当BS改变然后返回时,又增加了一行。
我想要的是一个空白行 - 空的组合框和文本框。
谢谢你的帮助!
班上:
Public Class PoList
Private _CountryID As Integer
Private _ProductID As Integer
Private _Quantity As Integer
Public Sub New(ByVal CountryID As Integer, ByVal ProductID As Integer, ByVal Quantity As Integer)
_CountryID = CountryID
_ProductID = ProductID
_Quantity = Quantity
End Sub
Private Sub New()
_CountryID = 1
_ProductID = 2
_Quantity = Nothing
End Sub
Public Property CountryID() As Integer
Get
Return _CountryID
End Get
Set(ByVal value As Integer)
_CountryID = value
End Set
End Property
Public Property ProductID() As Integer
Get
Return _ProductID
End Get
Set(ByVal value As Integer)
_ProductID = value
End Set
End Property
Public Property Quantity() As Integer
Get
Return _Quantity
End Get
Set(ByVal value As Integer)
_Quantity = value
End Set
End Property
Public Shared Function CreateNewPoList() As PoList
Return New PoList
End Function
End Class
Private Sub List_AddRow(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles AllListBindingSource.AddingNew
e.NewObject = PoList.CreateNewPoList
End Sub
创建一个新的内部列表:
AllList.Add(New List(Of PoList))
AllListBindingSource.AddNew()
AllListBindingSource.DataSource = AllList(TableCounter)
AddPoDetails.DataSource = AllListBindingSource
SelectedIndexChanged 事件:
AllListBindingSource.DataSource = AllList(AddPoList.SelectedIndex)
AddPoDetails.DataSource = Nothing
AddPoDetails.DataSource = AllListBindingSource