Validate value of column in every Datagridview row

Norman Maina
Norman Maina used Ask the Experts™
on
Hello,

I have a sql View that returns two columns - 'Item', 'Balance'

Then I have a datagridview with a comboboxcolumn 'Item' that matches the one in the view but is pulled from a different source.

I want to use the View to validate the datagridview column so that before a user saves that I can query every row and in the datagridview and check the 'balances' in every 'Item' is not greater than zero.

I am using a function but when i debig it-i find  its only checking the first row and not going to the next while I want it to check every row 'Item'  in the datagridview

see function code below:
Try
              For Each row As DataGridViewRow In SalesDtlDataGridView.Rows

                Dim cnn As New SqlConnection(My.Settings.TeaTrackingConnectionString)
                If cnn.State = ConnectionState.Closed Then cnn.Open()
                Dim cmd As New SqlCommand("select GardenDtlID,InvoiceNo,BalanceQty from dbo.vwInvoiceBalance where GardenDtlID=" + "'" + row.cells(2)Value.ToString + "'", cnn)
                Dim Inv As String
                Dim Bal As Int32
                Dim dr As SqlDataReader
                dr = cmd.ExecuteReader
                While dr.Read
                    Inv = dr("Item")
                    Bal = dr("Balance")
                End While
                If Bal <= 0 Then
                    Return False
                    Exit Function
                Else
                    Return True
                End If
            Next i
        Catch ex As Exception
            Return Nothing
            MessageBox.Show(ex.Message, "Tea Tracking | ValidateInvQty", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
u need to insert the validation code into the:

  While dr.Read
                    Inv = dr("Item")
                    Bal = dr("Balance")
                End While

so it should be something like:



  While dr.Read
                    Inv = dr("Item")
                    Bal = dr("Balance")

  If Bal <= 0 Then
'do whatever u want here
end if

                End While

Author

Commented:
sedgwick>
Is the same thing - I have used the results from the function in this line in the Save procedure:

Problem is that its looking at the first Item and then not checking the next item.

If Not SalesDtlDataGridView.CurrentRow Is Nothing And ValidateInvQty() = False Then
                    MessageBox.Show("Cannot Sell more than Item Quantity ", "Save", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    Exit Try
                    Exit Sub
                End If
Most Valuable Expert 2012
Top Expert 2014
Commented:
You have Exit Function and return statements in this block

                If Bal <= 0 Then
                    Return False
                    Exit Function
                Else
                    Return True
                End If

hence it exits the function. Change this code to some thing else (make grid row red etc).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial