Dear Sir,
I'm Trying to make the datagridview column "Quantity" not be minus with numericupdown and event button in VB.NET
so I want the "Qty" column datagridview1 to keep a minimum value of 1 in the btnMinusQty event.
One more way to check the datagridview has no data when using the event button (btnMinusQty & btnPlusQty).
Please Guide me
Public Class Form1
Private list As BindingList(Of Product) = Nothing
Private bindingSource As BindingSource = Nothing
Public Sub New()
InitializeComponent()
npQuantity.Maximum = Decimal.MaxValue
npQuantity.Minimum = 1
End Sub
' Retrieve product from selected row.
Private Function GetProduct() As Product
Return If(DataGridView1.SelectedCells.Count = 0, Nothing, TryCast(DataGridView1.SelectedCells(0).OwningRow.DataBoundItem, Product))
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnPlusQty_Click(sender As Object, e As EventArgs) Handles btnPlusQty.Click
' Change quantity here.
Dim product = GetProduct()
If product IsNot Nothing Then
' Update product's quantity.
product.Quantity += npQuantity.Value
' Do not forget to refresh grid.
DataGridView1.Refresh()
End If
End Sub
Private Sub btnMinusQty_Click(sender As Object, e As EventArgs) Handles btnMinusQty.Click
' Change quantity here.
Dim product = GetProduct()
If product IsNot Nothing Then
' Update product's quantity.
product.Quantity -= npQuantity.Value
' Do not forget to refresh grid.
DataGridView1.Refresh()
End If
End Sub
Private Sub BtnAddproduct_Click(sender As Object, e As EventArgs) Handles BtnAddproduct.Click
Dim List As New List(Of Product)()
List.Add(New Product() With {
.Code = "A",
.Quantity = 2
})
List.Add(New Product() With {
.Code = "B",
.Quantity = 2
})
Dim bindingList As New BindingList(Of Product)(List)
DataGridView1.AutoGenerateColumns = False
' Disable adding new row
DataGridView1.AllowUserToAddRows = False
' Create our medium between grid and collection
bindingSource = New BindingSource With {.DataSource = List}
DataGridView1.DataSource = bindingSource
End Sub
End Class
Friend Class Product
Public Property Code As String
Public Property Quantity As Decimal
End Class