wizzuu Posted May 28, 2015 Report Share Posted May 28, 2015 (edited) Boa tarde. Estou a fazer uma gestão de stocks, como apresentado em tópicos meus anteriores, e quero fazer um loop numa datagridview com valores ( valores da compra (quantidade) ) , verificando noutra datagridview ligada a uma base de dados, as linhas com a quantidade = 0 ( ou seja stock inexistente ). o código que tenho agora é este: Dim instrucao, prod, qtd, stocknome, dtgnome As String Dim i, stockqtd As Integer i = 0 For Each row As DataGridViewRow In DataGridView1.Rows stocknome = StocksDataGridView.Rows.Item(i).Cells(1).Value stockqtd = StocksDataGridView.Rows(i).Cells(2).Value.ToString dtgnome = row.Cells(1).Value.ToString MsgBox(stocknome & " " & dtgnome & " " & stockqtd) If stocknome = dtgnome Then If stockqtd <= "0" Then MsgBox("O produto " & stocknome & " não está disponível em Stock, por isso foi retirado do pedido.", MsgBoxStyle.Exclamation) stock_disponivel.Text = "n" End If Else prod = DataGridView1.Rows(i).Cells(1).Value.ToString qtd = DataGridView1.Rows(i).Cells(3).Value instrucao = "UPDATE Stocks SET Quantidade=Quantidade-'" & qtd & "' WHERE Nome='" & prod & "'" Me.StocksTableAdapter1.Adapter.SelectCommand.CommandText = instrucao Me.StocksTableAdapter1.Update(Me.StocksDataSet.Stocks) Me.StocksTableAdapter1.Fill(Me.StocksDataSet.Stocks) End If i += 1 Next dá-me o erro: O Índice estava fora do intervalo. Tem de ser não negativo e inferior ao tamanho da colecção. aqui: stocknome = StocksDataGridView.Rows.Item(i).Cells(1).Value Podem-me ajudar? :S Cumprimentos Edited May 28, 2015 by apocsantos geshi Link to comment Share on other sites More sharing options...
jmsmoreira Posted May 28, 2015 Report Share Posted May 28, 2015 (edited) O indice de row começa no 0, logo se tens 10 linhas a ultima é a 9... Costumo usar um ciclo for next tipo for i = 0 to DataGridView1.Rowcount -1 Edited May 28, 2015 by jmsmoreira Link to comment Share on other sites More sharing options...
wizzuu Posted May 28, 2015 Author Report Share Posted May 28, 2015 O indice de row começa no 0, logo se tens 10 linhas a ultima é a 9... Costumo usar um ciclo for next tipo for i = 0 to DataGridView1.Rowcount -1 Obrigado pela resposta, já tinha tentado assim mas talvez não tenha posto no sítio correcto. Vou ver se dou umas voltas a isto para descobrir, depois digo mais alguma coisa. Cumprimentos Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now