验证datagrid单元格与另一个单元格和函数的总和进行比较

时间:2016-05-24 13:17:35

标签: sql vb.net function validation datagrid

当我在DataGridView1.Columns(6)的单元格中输入值时,如果DataGridView1.Columns(7)的单元格值大于DataGridView1.Columns的单元格值,我有一个想要检查的数据网格(6)加上一个函数的结果,该函数的参数为​​DataGridView1.Columns(1),并从DataGridView1.Columns(6)中获取先前插入值的总和。

我不知道的事情:

  • 在哪里进行此项检查以及如何进行检查?
  • 如何在datagrid的每一行的检查公式中分配函数的参数?

功能是:

Public Shared Function getproddeclarata(ByVal nrfpo As String) As Integer
    Dim total As Integer
    Dim con As New SqlConnection
    Try
        con = New SqlConnection("Data Source=SVNAV;Initial Catalog=NAV_Vermorel_Live;User ID=sa;Password=1234")
        Using cmd As SqlCommand = New SqlCommand("SELECT cast(SUM([Productie Declarata]) as Decimal(18,2)) as TotalOreDeclarate FROM [SC Vermorel SRL$ProductieZilnica] WHERE FPO = @nrfpo", con)
            cmd.Parameters.AddWithValue("@nrfpo", nrfpo)
            con.Open()
            total = Convert.ToInt32(cmd.ExecuteScalar())
            con.Close()
        End Using
    Catch ex As Exception
        Console.WriteLine(ex.Message)
    Finally
        If con IsNot Nothing Then
            If con.State = ConnectionState.Open Then                    '
                con.Close()
            End If
            con.Dispose()
        End If
    End Try
    Return total
End Function

1 个答案:

答案 0 :(得分:0)

为此,您可以将活动DataGridView1_CellEndEdit视为:

Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Try
            Dim rw As DataGridView = DirectCast(sender, DataGridView)

          If e.ColumnIndex = 6 Then
            If IsNumeric (rw(e.ColumnIndex, e.RowIndex).Value) andalso rw(e.ColumnIndex, e.RowIndex).Value)>0  Then
                If rw(7, e.RowIndex).Value > rw(e.ColumnIndex, e.RowIndex).Value + getproddeclarata(rw(1, e.RowIndex).Value.ToString()) Then

                    'Same code here

                End If
            End If
        End If

        Catch ex As Exception

        End Try
End Sub