Link to home
Start Free TrialLog in
Avatar of Norman Maina
Norman MainaFlag for Kenya

asked on

Validate value of column in every Datagridview row

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

Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

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
Avatar of Norman Maina

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial