Gran problema de cálculo total de datagridview

programación


Hola.
Cuando inserto un subtotal en la vista de cuadrícula de datos, el código vuelve a calcular los cambios totales generales.
mi bucle. cuenta todo
Necesito tu ayuda o tus ideas por favor.

Lo que he probado:

V.B.
Private Sub BtnSum_Click(sender As Object, e As EventArgs) Handles BtnSum.Click
    Dim somme As Double
    For x As Integer = 0 To DgvDevisClent.Rows.Count - 1
        somme += CType(DgvDevisClent.Rows(x).Cells(4).Value, Double)
    Next
    somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))
    DgvDevisClent.Rows.Insert(DgvDevisClent.CurrentRow.Index + 1, 1)
    DgvDevisClent.CurrentCell = DgvDevisClent.Rows(DgvDevisClent.CurrentRow.Index + 1).Cells(0)
    DgvDevisClent.CurrentRow.Cells(4).Value = somme
    DgvDevisClent.Focus()
End Sub

Solución 1

Tienes la siguiente línea en tu código:

V.B.
somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))

Entonces, convierte el valor doble en una cadena, para luego poder convertirlo nuevamente a doble, para usarlo en la llamada a String.Format, lo cual es un poco inútil. Pero luego usas esa cadena para establecer el valor de somme, que se declara como doble. Todo esto no tiene sentido, así que elimina esa línea de código.

Solución 2

Basado en tu comentario –

Cita:

Cuando actualizo el código con un evento de datagridview, el total general cambia en mi etiqueta
ejemplo
primer artículo $1
segundo artículo $2
Subtotal $3
Mi etiqueta muestra $6 o lugar para mostrar $3

No está borrando el total original para mostrar el total actualizado cuando actualiza su cuadrícula de datos.

V.B.
'Clear your label of all totals...
YourLabel.Text = ""

'Recalculate the total and update your label with the correct total...
Dim somme As Double
For x As Integer = 0 To DgvDevisClent.Rows.Count - 1
    somme += CType(DgvDevisClent.Rows(x).Cells(4).Value, Double)
Next
somme = String.Format("{0:##,##0.000}", CDbl(somme.ToString))

'Display the updated total in your label, PAY ATTENTION to the above solution and comments though...
YourLabel.Text = somme

コメント

タイトルとURLをコピーしました