Bonjour ,
comment j'ajoute comme cette ligne de total en bas du datagridview qui sera stable même si l'utilisateur utilise le scrollbar ?
Merci d'avance.
Bonjour ,
comment j'ajoute comme cette ligne de total en bas du datagridview qui sera stable même si l'utilisateur utilise le scrollbar ?
Merci d'avance.
Bonjour
A Première vue elle ne fait pas partie du datagrid, mais d'un second limité a une ligne. Ce dernier reçoit les valeur de calcul du premier datagridview
La Connaissance est comme la joie elle s'accroît en la partageant!
bonjour
Tu peux ajouter des Controls en bas du dgv pour les totaux ...
voici un code exemple succinct de ce qu'il y a lieu de se taper comme code :
NB : la bd StockProduits est en pièce jointe ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 Imports System.Data.OleDb Public Class FormTotalControl Private lbTotal As Label = New Label() Private tbPrix As TextBox = New TextBox() Private tbPrixTTC As TextBox = New TextBox() 'index des colonnes concernées Dim colID As Integer Dim colPrix As Integer Dim colTaxe As Integer Dim colPrixTTC As Integer 'colonne calculée en vol Private dt As New DataTable Private da As New OleDbDataAdapter Private conn As New OleDbConnection Private Sub FormTotalControl_Load(sender As Object, e As System.EventArgs) Handles Me.Load conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\HUSSEIN\Desktop\WinDGV\WinSubTotalDGV\StockProduits.mdb;Persist Security Info=True" ' select command da.SelectCommand = New OleDbCommand("SELECT ID, Prix, Taxe, PrixTTC FROM Produits") da.SelectCommand.Connection = conn da.Fill(dt) Me.dgv.DataSource = dt ' recupere index des colonnes concernées colID = Me.dgv.Columns(0).Index colPrix = Me.dgv.Columns(1).Index colTaxe = Me.dgv.Columns(2).Index colPrixTTC = Me.dgv.Columns(3).Index 'ajout des controls en bas du dgv AddControls() End Sub Private Sub AddControls() lbTotal.Text = "Total" lbTotal.BorderStyle = BorderStyle.FixedSingle lbTotal.BackColor = Color.Red lbTotal.ForeColor = Color.Yellow lbTotal.Height = tbPrix.Height lbTotal.AutoSize = False lbTotal.TextAlign = ContentAlignment.MiddleCenter Dim X As Integer = Me.dgv.GetCellDisplayRectangle(colID, -1, True).Location.X lbTotal.Width = Me.dgv.Columns(colID).Width lbTotal.Location = New Point(X, Me.dgv.Height - lbTotal.Height) Me.dgv.Controls.Add(lbTotal) tbPrix.BorderStyle = BorderStyle.FixedSingle tbPrix.ForeColor = Color.Red tbPrix.Width = Me.dgv.Columns(colPrix).Width X = Me.dgv.GetCellDisplayRectangle(colPrix, -1, True).Location.X tbPrix.Location = New Point(X, Me.dgv.Height - tbPrix.Height) Me.dgv.Controls.Add(tbPrix) tbPrixTTC.BorderStyle = BorderStyle.FixedSingle tbPrixTTC.ForeColor = Color.Red tbPrixTTC.Width = Me.dgv.Columns(colPrixTTC).Width X = Me.dgv.GetCellDisplayRectangle(colPrixTTC, -1, True).Location.X tbPrixTTC.Location = New Point(X, Me.dgv.Height - tbPrixTTC.Height) Me.dgv.Controls.Add(tbPrixTTC) End Sub Private Sub DGV_CellPainting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles dgv.CellPainting If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Return Dim prix As Double Dim taxe As Double Dim prixTTC As Double Dim totalPrix As Double = 0.0 Dim totalPrixTTC As Double = 0.0 For i As Integer = 0 To Me.dgv.Rows.Count - 2 prix = 0.0 prixTTC = 0.0 taxe = 0.0 If Not IsDBNull(Me.dgv(colPrix, i).Value) Then prix = Convert.ToDouble(Me.dgv(colPrix, i).Value) End If If Not IsDBNull(Me.dgv(colTaxe, i).Value) Then taxe = Convert.ToDouble(Me.dgv(colTaxe, i).Value) End If prixTTC = prix * (1 + taxe / 100) Me.dgv(colPrixTTC, i).Value = prix * (1 + taxe / 100) totalPrix += prix totalPrixTTC += prixTTC Next tbPrix.Text = totalPrix.ToString() tbPrixTTC.Text = totalPrixTTC.ToString() lbTotal.Update() tbPrix.Update() tbPrixTTC.Update() 'Dim X As Integer = Me.dgv.GetCellDisplayRectangle(colID, -1, True).Location.X 'lbTotal.Width = Me.dgv.Columns(0).Width 'lbTotal.Location = New Point(0, Me.dgv.Height - lbTotal.Height) 'lbTotal.Text = "Total" 'tbPrix.Width = Me.dgv.Columns(colPrix).Width 'X = Me.dgv.GetCellDisplayRectangle(colPrix, -1, True).Location.X 'tbPrix.Location = New Point(X, Me.dgv.Height - tbPrix.Height) 'tbPrixTTC.Width = Me.dgv.Columns(colPrixTTC).Width 'X = Me.dgv.GetCellDisplayRectangle(colPrixTTC, -1, True).Location.X 'tbPrixTTC.Location = New Point(X, Me.dgv.Height - tbPrixTTC.Height) End Sub End Class
bon code...
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager