1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If e.ColumnIndex < 0 Or e.RowIndex < 0 Then
Exit Sub
End If
'Obtenir le composant
Dim grid As DataGridView = DirectCast(sender, DataGridView)
'Définir le nom des colonnes (à changer en fonction de tes propres paramètres)
Dim colonneQuantite As String = "ColonneQuantite"
Dim colonnePrixUnit As String = "ColonnePrixUnit"
Dim colonneTotal As String = "ColonneTotal"
'Obtenir la cellule et son contenu
Dim cellule As DataGridViewCell = grid.Item(e.ColumnIndex, e.RowIndex)
Dim contenuCellule As String = CStr(cellule.Value)
'En fonction de la colonne où se trouve la cellule
Dim nomColonne As String = grid.Columns(e.ColumnIndex).Name
Select Case nomColonne
Case colonneQuantite
Try
'Calculer le total
Dim celluleMultiplicande As DataGridViewCell = grid.Item(colonnePrixUnit, e.RowIndex)
Dim celluleTotal As DataGridViewCell = grid.Item(colonneTotal, e.RowIndex)
If celluleMultiplicande.Value.ToString <> "" Then
Try
Dim multiplicateur As Decimal = CDec(RectifierSeparateur(contenuCellule))
Dim multiplicande As Decimal = CDec(RectifierSeparateur(celluleMultiplicande.Value.ToString))
celluleTotal.Value = (multiplicande * multiplicateur).tostring
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Catch ex As Exception
'
End Try
Case colonnePrixUnit
If contenuCellule <> "" Then
Try
'Appliquer le format
Dim mutiplicande As Decimal = CDec(RectifierSeparateur(contenuCellule))
cellule.Value = FormatMonnaie(mutiplicande)
'Calculer le total
Dim celluleMultiplicateur As DataGridViewCell = grid.Item(colonneQuantite, e.RowIndex)
Dim celluleTotal As DataGridViewCell = grid.Item(colonneTotal, e.RowIndex)
If celluleMultiplicateur.Value.ToString <> "" Then
Try
Dim multiplicateur As Decimal = CDec(RectifierSeparateur(celluleMultiplicateur.Value.ToString))
celluleTotal.Value = (mutiplicande * multiplicateur).ToString
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Catch ex As Exception
'
End Try
End If
Case colonneTotal
If contenuCellule <> "" Then
Try
'Appliquer le format
Dim total As Decimal = CDec(RectifierSeparateur(contenuCellule))
cellule.Value = FormatMonnaie(total)
'Calculer la somme de la colonne Total et placer le résultat dans Label1
Label1.Text = FormatMonnaie(CalculSomme("ColonneTotal"))
Catch ex As Exception
'
End Try
End If
End Select
End Sub
Private Function RectifierSeparateur(valeur As String) As String
Dim separateur As String = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator
valeur = valeur.Replace(".", separateur)
valeur = valeur.Replace(",", separateur)
Return valeur
End Function
Private Function FormatMonnaie(valeur As Decimal) As String
Return String.Format("{0:0.00} ", valeur)
End Function
Private Function CalculSomme(nomColonne As String) As Decimal
'En entrée le Nom (propriété Name) de la colonne
Dim Total As Decimal = 0
'Evaluer toutes les rangées du tableau dans la colonne en cours
For Each row As DataGridViewRow In DataGridView1.Rows
'Récupération de la cellule
Dim cellule As DataGridViewCell = DataGridView1.Item(nomColonne, row.Index)
Try
'Suppression du symbole
Dim contenu As String = cellule.Value.ToString.Replace("", "")
'Tentative de conversion en décimal
Dim valeur As Decimal
If Decimal.TryParse(contenu, valeur) = True Then
'Addition
Total += valeur
End If
Catch ex As Exception
'
End Try
Next
'En sortie le total (décimal)
Return Total
End Function |
Partager