Somando horas no DataGridView Visual Basic 2010
19/06/2011
0
Boa noite.Tenho um DataGridView que busca no bancos os dados após consulta.
No DataGrid possui uma coluna chamada horas trabalhadas e ao realizar
o filtro gostaria de colocar um textbox com a soma dessa coluna.
Estou utilizando o Visual basic 2010. Tentei o seguinte código no click do botão soma:
Dim resultado As TimeSpan
For Each col As DataGridViewRow In DgvProjeto.Rows
resultado = resultado + col.Cells(10).Value
Next
TxtTotal.Text = resultado.ToString
End Sub Porém aparece essa mensagem de erro: "Conversão especificada não é válida." No banco o tipo do dado é DateTime Poderiam me ajudar?Grato.
For Each col As DataGridViewRow In DgvProjeto.Rows
resultado = resultado + col.Cells(10).Value
Next
TxtTotal.Text = resultado.ToString
End Sub Porém aparece essa mensagem de erro: "Conversão especificada não é válida." No banco o tipo do dado é DateTime Poderiam me ajudar?Grato.
Cláudio Freitas
Curtir tópico
+ 0
Responder
Posts
19/06/2011
Cláudio Freitas
Pessoal recebi uma ajuda com um código que está dando um erro, segue abaixo para quem puder me ajudar.
O código que me foi enviado abaixo com as alterações deu o seguinte erro: "Conversão inválida de 'DateTime' em 'Double'." Private Function Total() As Double
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To DgvProjeto.Rows.Count - 1
tot = tot + Convert.ToDouble(DgvProjeto.Rows(i).Cells("HorasTrabalhadas").Value)
Next i
Return tot
End Function Private Sub PcxTotalHoras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PcxTotalHoras.Click
If DgvProjeto.Rows.Count > 0 Then
TxtTotal.Text = Total().ToString("c")
End If
End Sub Com esse erro fiz algumas alterações, porém, ocorreu outra mensagem de erro: "A conversão da cadeia de caracteres '00:00:0001/01/1900' no tipo 'Date' não é válida." Segue o código com as modificações: Private Function Total() As Double
'Dim tot As Double = 0
Dim tot As Date
Dim i As Integer = 0
For i = 0 To DgvProjeto.Rows.Count - 1
tot = tot + Convert.ToDateTime(DgvProjeto.Rows(i).Cells("HorasTrabalhadas").Value)
Next i
Return tot.ToOADate()
End Function
Private Sub PcxTotalHoras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PcxTotalHoras.Click
If DgvProjeto.Rows.Count > 0 Then
TxtTotal.Text = Total().ToString("d")
End If End Sub Grato,
O código que me foi enviado abaixo com as alterações deu o seguinte erro: "Conversão inválida de 'DateTime' em 'Double'." Private Function Total() As Double
Dim tot As Double = 0
Dim i As Integer = 0
For i = 0 To DgvProjeto.Rows.Count - 1
tot = tot + Convert.ToDouble(DgvProjeto.Rows(i).Cells("HorasTrabalhadas").Value)
Next i
Return tot
End Function Private Sub PcxTotalHoras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PcxTotalHoras.Click
If DgvProjeto.Rows.Count > 0 Then
TxtTotal.Text = Total().ToString("c")
End If
End Sub Com esse erro fiz algumas alterações, porém, ocorreu outra mensagem de erro: "A conversão da cadeia de caracteres '00:00:0001/01/1900' no tipo 'Date' não é válida." Segue o código com as modificações: Private Function Total() As Double
'Dim tot As Double = 0
Dim tot As Date
Dim i As Integer = 0
For i = 0 To DgvProjeto.Rows.Count - 1
tot = tot + Convert.ToDateTime(DgvProjeto.Rows(i).Cells("HorasTrabalhadas").Value)
Next i
Return tot.ToOADate()
End Function
Private Sub PcxTotalHoras_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PcxTotalHoras.Click
If DgvProjeto.Rows.Count > 0 Then
TxtTotal.Text = Total().ToString("d")
End If End Sub Grato,
Responder
Clique aqui para fazer login e interagir na Comunidade :)