在 vb.net 中使用相同的表单查看/编辑时如何禁用 datagridview rowvalidating 事件

问题描述 投票:0回答:1

当使用相同的表单在 vb.net 中查看/编辑时,我尝试禁用 datagridview rowvalidating 事件。

所以我用一个

FormTransaction
来添加和查看/编辑

所以当我点击按钮时出现错误

BtnViewEdit

代码有问题吗?。

请指导我

谢谢

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

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

如果我使用按钮

ADD
然后FormTransaction出现 使用空的 dataGridView,所以在这里我输入出站事务,并使用 rowvalidating 事件来控制
Qty
是否大于
Qty_Stock

代码产品 数量 数量_库存

如果我使用按钮

VIEW/EDIT
然后FormTransaction出现 但出现错误,因为它读取的 Codeproduct
1000
没有
Qty_Stock
。因此,当表单仅用作视图/编辑时,我可以如何禁用 rowvalidating 事件

代码产品 数量 数量_库存
1000 20 0
2000 30 40

.net vb.net winforms events datagridview
1个回答
0
投票

您在

BtnAdd
BtnViewEdit
中打开表格的方式不同。您可以使用它来设置布尔变量(仅显示相关部分):

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
    End Sub

    Public Sub New(Invno As Product)
        Me.New
        showMessages = True '<===========
        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
© www.soinside.com 2019 - 2024. All rights reserved.