Jump to content

Recommended Posts

Posted

é assim eu tenho uma datagrid, com items, onde uma coluna tem o numero de items, item 1, item 2 etc... e quero ver o valor valor maximo do item que la tiver, por exemplo se o maior numero for 5, enviar para uma textbox o numero 5.

LM

Posted

Private Function getMaxDataGridViewValue(ByVal columnNumber As Integer, ByVal aDataGridView As DataGridView) As String



Dim maxValue As Double = 0

Dim count As Integer = 0

count = aDataGridView.RowCount

For row = 1 To count

If aDataGridView.Item(columnNumber, row).Value > maxValue Then

maxValue = aDataGridView(columnNumber, row).Value

End If

Next

Return maxValue.ToString

End Function

End Class

depois fazes isto
testbox1.text=getMaxDataGridViewValue(1, DataGridView1)  -onde 1 e a tua coluna que queres o maximo do valor
Posted

Estás a ir para um index de coluna que não existe, se bem me lembro as colunas e as linhas começam em ZERO e não em Um, e terminam em N-1.

Como tens o ciclo for de 1 a N, cais fora do array.

Experimenta fazer:

If aDataGridView.Item(columnNumber, row -1 ).Value > maxValue

Ricardo Timóteo

Posted

Experimenta lá assim substituindo o zero (row.Cells(0).Value) pelo index ou nome da tua coluna.

    Private Function getMaximumValue() As Integer
        Dim result As Integer = 0

        Try

            ' Ciclo em todas as linhas
            For Each row As DataGridViewRow In Me.DataGridView1.Rows
                If Not row.IsNewRow Then

                    ' Verifica se o valor é superior ao actual
                    If row.Cells(0).Value > result Then
                        result = row.Cells(0).Value
                    End If

                End If
            Next

            Return result

        Catch ex As Exception
            Return 0
        End Try

    End Function

Depois utilizas:

Me.TexBox1.Text = getMaximumValue()
Posted

Resultou....

Tou só aqui com mais um problema, que é ao actualizar, ao actualizar guarda todos menos o item com o numero mais baixo, ou seja, em vez de guardar 2,3,4,5 guarda apenas o 3,4,5. Alguem sabe doque poderá ser?

LM

Posted

Tou só aqui com mais um problema, que é ao actualizar, ao actualizar guarda todos menos o item com o numero mais baixo, ou seja, em vez de guardar 2,3,4,5 guarda apenas o 3,4,5. Alguem sabe doque poderá ser?

Não entendi nada!

Explica melhor, cria um novo tópico e mostra o código que estás a utilizar.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.