Antonio2307 Posted June 2, 2015 at 03:08 PM Report #584101 Posted June 2, 2015 at 03:08 PM (edited) Amigos, Estou tentando fazer uma rotina para calcular a soma de campos selecionados em um DataGridView usando VB 2008 Seleciono varias células em uma coluna, quando a coluna tem valores numéricos o calculo sai correto, mas se eu selecionar uma célula com um texto (string) vai dar erro, como posso saber que o usuário selecionou campos string e não numéricos. vejam a rotina abaixo. Private Sub SomaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SomaToolStripMenuItem.Click Dim FSoma As Double = 0 Try Clipboard.SetDataObject(DataGridView.GetClipboardContent()) VetorClipboard = Split(Clipboard.GetText, vbCrLf) For i = 0 To DataGridView.GetCellCount(DataGridViewElementStates.Selected) - 1 If VetorClipboard(i) <> "" Then FSoma = FSoma + VetorClipboard(i) End If Next MsgBox( "Somatria = " & Format(FSoma, "###,###,###,###.##")) Catch ex As System.Runtime.InteropServices.ExternalException MsgBox( "Falha coletando dados Tente novamente. " & ex.Message, MsgBoxStyle.Information) End Try End Sub Agradeço antecipado, Antonio Edited June 2, 2015 at 07:55 PM by apocsantos tag code + geshi
Solution vikcch Posted June 3, 2015 at 02:51 PM Solution Report #584175 Posted June 3, 2015 at 02:51 PM Olá, em vez de comparares: If VetorClipboard(i) <> "" Then podes usar o metodo tryParse para converteres para double. ficaria qualquer coisa como isto: Dim number As Double If Double.TryParse(VetorClipboard(i), number) Then FSoma = FSoma + number End If se tiveres problemas com o separador decimal / valores decimais tem a ver com as definições regionais, podes especificar lá nos argumentos do metodo TryParse... vê o link
Antonio2307 Posted June 3, 2015 at 05:31 PM Author Report #584186 Posted June 3, 2015 at 05:31 PM Obrigado amigão Aprendi mais uma. Mas se o usuário selecionar campos de uma coluna que sejam alfanuméricos (string ) vai dar erro. gostaria de saber , como reconhecer que o usuário selecionou campos string e não numéricos. Agradeço antecipado Antonio
Antonio2307 Posted June 3, 2015 at 07:15 PM Author Report #584191 Posted June 3, 2015 at 07:15 PM (edited) Consegui fazer graças a dica do colega. ficou assim: Private Sub SomaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SomaToolStripMenuItem.Click Dim FSoma As Double = 0 Dim Number As Double Try Clipboard.SetDataObject(DataGridView.GetClipboardContent()) VetorClipboard = Split(Clipboard.GetText, vbCrLf) For i = 0 To DataGridView.GetCellCount(DataGridViewElementStates.Selected) - 1 If Double.TryParse(VetorClipboard(i), Number) Then FSoma = FSoma + Number Else MsgBox( "No posso somar campos alfanumericos. Selecione uma coluna com valores.", Number) Exit For End If Next If FSoma <> 0 Then MsgBox( "Somatria = " & Format(FSoma, "###,###,###,###.##")) End If Catch ex As System.Runtime.InteropServices.ExternalException MsgBox( "Falha coletando dados Tente novamente. " & ex.Message, MsgBoxStyle.Information) End Try End Sub Edited June 3, 2015 at 09:33 PM by apocsantos tag code + geshi
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