Luis Marques Posted April 7, 2009 at 04:35 PM Report #255717 Posted April 7, 2009 at 04:35 PM Boas pessoal, preciso de fazer tipo um select para ver o valor maximo numa datagridview, ou seja se o valor maximo que la está se é 1,2, ou 3, etc... Já tive a pesquisar mas não achei nada. LM
scorch Posted April 7, 2009 at 04:40 PM Report #255718 Posted April 7, 2009 at 04:40 PM Isto? Dim var As Integer = ContactosDataGridView.RowCount PS: Não respondo a perguntas por mensagem que podem ser respondidas no fórum.
Luis Marques Posted April 7, 2009 at 04:48 PM Author Report #255721 Posted April 7, 2009 at 04:48 PM nao sei, nao faço mesmo ideia de como isso se faz! isso é o indicado para ver o valor maximo? eu queria que me devolve se para o textbox. LM
esquima Posted April 7, 2009 at 04:51 PM Report #255723 Posted April 7, 2009 at 04:51 PM o valor maximo de que?
Luis Marques Posted April 7, 2009 at 04:53 PM Author Report #255724 Posted April 7, 2009 at 04:53 PM é 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
esquima Posted April 7, 2009 at 04:57 PM Report #255727 Posted April 7, 2009 at 04:57 PM 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
Luis Marques Posted April 7, 2009 at 05:16 PM Author Report #255730 Posted April 7, 2009 at 05:16 PM tame a dar erro aqui: If aDataGridView.Item(columnNumber, row).Value > maxValue Then Erro: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index LM
Luis Marques Posted April 7, 2009 at 06:43 PM Author Report #255744 Posted April 7, 2009 at 06:43 PM Esquima sabes do que se trata? LM
nokPT Posted April 7, 2009 at 06:51 PM Report #255746 Posted April 7, 2009 at 06:51 PM 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
Luis Marques Posted April 7, 2009 at 07:20 PM Author Report #255753 Posted April 7, 2009 at 07:20 PM assim já nao está a dar erro! Nao me esta é a enviar o maximo valor, está a mostrar o valor 0 LM
jpaulino Posted April 7, 2009 at 10:37 PM Report #255800 Posted April 7, 2009 at 10:37 PM 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()
Luis Marques Posted April 7, 2009 at 10:47 PM Author Report #255803 Posted April 7, 2009 at 10:47 PM 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
jpaulino Posted April 8, 2009 at 07:21 AM Report #255839 Posted April 8, 2009 at 07:21 AM 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.
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