I'm trying to disable the datagridview rowvalidating event when using the same form to view/edit in vb.net.
so I use one FormTransaction
to do adding and viewing/editing
so an error appears when I click the button BtnViewEdit
.
Is there something wrong with the code?.
Please Guide Me
Thanks
Code In FormMain
Public Class FormMain
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim list = New List(Of Product)() From {
New Product With {
.Codeproduct = "1000",
.Qty = 20,
.Qty_Stock = 50
},
New Product With {
.Codeproduct = "2000",
.Qty = 30,
.Qty_Stock = 40
}
}
Dim bindingList As New BindingList(Of Product)(CType(list, IList(Of Product)))
Dim source = New BindingSource(bindingList, Nothing)
DataGridView1.DataSource = source
End Sub
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
Dim frm = New FormTransaction
frm.ShowDialog()
End Sub
Private Sub BtnViewEdit_Click(sender As Object, e As EventArgs) Handles BtnViewEdit.Click
Dim Invno = DirectCast(DataGridView1.SelectedRows(0).DataBoundItem, Product)
If DataGridView1.SelectedRows.Count > 0 Then ' make sure user select at least 1 row
Using frm = New FormTransaction(Invno)
If frm.ShowDialog() = DialogResult.OK Then
End If
End Using
End If
End Sub
End Class
Public Class Product
Public Property Codeproduct() As String
Public Property Qty() As Integer
Public Property Qty_Stock() As Integer
End Class
Code In FormTransaction
Public Class FormTransaction
Private list As New List(Of Product)() From {
New Product With {
.Codeproduct = "1000",
.Qty = 20,
.Qty_Stock = 0
},
New Product With {
.Codeproduct = "2000",
.Qty = 30,
.Qty_Stock = 40
}
}
Private listnew As New List(Of Product)()
Public Sub New()
InitializeComponent()
Dim bindingList As New BindingList(Of Product)(CType(listnew, IList(Of Product)))
Dim source = New BindingSource(bindingList, Nothing)
DataGridView1.DataSource = source
End Sub
Public Sub New(Invno As Product)
Me.New
DataGridView1.DataSource = list
End Sub
Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
Dim transferredQTY As Integer
Dim onHandQTY As Integer
With DataGridView1.Rows(e.RowIndex)
Try
transferredQTY = Convert.ToInt32(.Cells("Qty").Value)
Catch ex As Exception
transferredQTY = 0
End Try
Try
onHandQTY = Convert.ToInt32(.Cells("Qty_Stock").Value)
Catch ex As Exception
onHandQTY = 0
End Try
If transferredQTY > onHandQTY Then
MessageBox.Show("Please check don't Have sufficient quantity Stock" & "Only have Stock " & onHandQTY & " Pcs", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End With
End Sub
End Class
If I use Button ADD
Then the FormTransaction appears
with an empty dataGridView so here I input the outbound transaction and I use the rowvalidating event to control if Qty
greater than Qty_Stock
CodeProduct | Qty | Qty_Stock |
---|---|---|
If I use Button VIEW/EDIT
Then the FormTransaction appears
But an error appears because it reads for Codeproduct 1000
don't have Qty_Stock
.So here's how I can disable rowvalidating events when the form is only used as a view/edit
CodeProduct | Qty | Qty_Stock |
---|---|---|
1000 | 20 | 0 |
2000 | 30 | 40 |
You are opening the form differently in BtnAdd
and BtnViewEdit
. You can use this fact to set a Boolean variable (only showing the relevant parts):
Public Class FormTransaction
...
Private showMessages As Boolean '<=========== is False when the Form is created.
Public Sub New()
InitializeComponent()
Dim bindingList As New BindingList(Of Product)(CType(listnew, IList(Of Product)))
Dim source = New BindingSource(bindingList, Nothing)
DataGridView1.DataSource = source
showMessages = True '<========== Called from BtnAdd
End Sub
Public Sub New(Invno As Product)
Me.New
showMessages = False '<=========== Called from BtnViewEdit
DataGridView1.DataSource = list
End Sub
Private Sub DataGridView1_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.RowValidating
Dim transferredQTY As Integer
Dim onHandQTY As Integer
With DataGridView1.Rows(e.RowIndex)
...
If showMessages AndAlso transferredQTY > onHandQTY Then '<===========
MessageBox.Show("Please check don't Have sufficient quantity Stock" & "Only have Stock " & onHandQTY & " Pcs", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
e.Cancel = True
End If
End With
End Sub
End Class