Bonjour à tous!
alors, voilà, petite question, savez-vous si il est possible de faire un cellvaluechanged sur seulement une partie du datagridview?
et si c'est possible, comment faire?
merci d'avance!
Bonjour à tous!
alors, voilà, petite question, savez-vous si il est possible de faire un cellvaluechanged sur seulement une partie du datagridview?
et si c'est possible, comment faire?
merci d'avance!
Bonjour,
Cela n'est pas possible de base, tu devras implémenter l'évènement pour toute la datagrid et tester à l'intérieur si la cellule modifiée est dans la colonne que tu souhaites (ou la ligne).
Si par exemple, tu veux une action uniquement si la cellule modifiée est dans la première colonne, cela donnerait:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 Private Sub dgv_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellValueChanged If e.ColumnIndex = 0 Then 'Ton traitement End If End Sub
donc il suffit que je rajoute ce code:
pour lui demander de n'agir qu'en cas de modifs sur cette colonne (suivant l'exemple)... je teste ça de suite, merci!
Code : Sélectionner tout - Visualiser dans une fenêtre à part If e.ColumnIndex = 0 Then
euh, juste une question, est-il possible de ne le faire que pour la ligne active? parce que j'ai un fichier de +/- 250 lignes sur 34 colonnes de données à rentrer et c'est plutôt long de faire les calculs sur toutes les lignes à chaque fois (15-20sec sur un cpu E-350...)
mon code:
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 Private Sub CellValueChanged(ByVal sender As Object, _ ByVal e As DataGridViewCellEventArgs) Handles PériodesDataGridView.CellValueChanged If e.ColumnIndex >= 3 AndAlso e.ColumnIndex <= 36 Then 'compteur nombre de jours pour moyenne For Each row As DataGridViewRow In PériodesDataGridView.Rows Dim compt As Double For Each cell As DataGridViewCell In row.Cells If cell.ColumnIndex >= 3 AndAlso cell.ColumnIndex <= 36 Then If Not IsDBNull(cell.Value) Then compt += 1 End If Next row.Cells(46).Value = compt compt = 0 Next 'calcul de la moyenne For Each row As DataGridViewRow In PériodesDataGridView.Rows Dim total As Double For Each cell As DataGridViewCell In row.Cells If cell.ColumnIndex >= 3 AndAlso cell.ColumnIndex <= 36 Then If Not IsDBNull(cell.Value) Then total += cell.Value End If Next row.Cells(37).Value = total row.Cells(38).Value = total / row.Cells(46).Value total = 0 Next 'calcul de l'écart For Each row As DataGridViewRow In PériodesDataGridView.Rows For Each cell As DataGridViewCell In row.Cells If IsDBNull(row.Cells(39).Value) Then row.Cells(40).Value = 0 Else row.Cells(40).Value = row.Cells(39).Value - row.Cells(37).Value End If Next Next 'calcul du nombre de jours For Each row As DataGridViewRow In PériodesDataGridView.Rows For Each cell As DataGridViewCell In row.Cells If IsDBNull(row.Cells(40).Value) Or IsDBNull(row.Cells(38).Value) Then row.Cells(41).Value = "" Else row.Cells(41).Value = row.Cells(40).Value / row.Cells(38).Value End If Next Next 'pour calcul ok depan For Each row As DataGridViewRow In PériodesDataGridView.Rows For Each cell As DataGridViewCell In row.Cells If IsDBNull(row.Cells(40).Value) Then row.Cells(45).Value = "pas de moyenne" Else row.Cells(45).Value = row.Cells(40).Value - (row.Cells(44).Value * row.Cells(38).Value) End If Next Next 'affichage ok, dépannage For Each row As DataGridViewRow In PériodesDataGridView.Rows For Each cell As DataGridViewCell In row.Cells If row.Cells(45).Value < 1 Then row.Cells(42).Value = "Dépannage" 'ElseIf row.Cells(14).Value < TextBox1.Text + 2 And row.Cells(14).Value > TextBox1.Text Then 'row.Cells(15).Value = "attention" Else row.Cells(42).Value = "ok" End If Next Next 'reste For Each row As DataGridViewRow In PériodesDataGridView.Rows For Each cell As DataGridViewCell In row.Cells row.Cells(43).Value = (row.Cells(40).Value - (row.Cells(44).Value * row.Cells(38).Value)) / 10 Next Next End If End Sub
Si tu veux mettre à jour le calcul seulement sur la ligne modifiée, travaille avecRemplace
Code : Sélectionner tout - Visualiser dans une fenêtre à part e.RowIndex
par
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 For Each row As DataGridViewRow In PériodesDataGridView.Rows (...) Next
Ainsi tu ne traite que la ligne qui a subi une modification.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 dim row As DataGridViewRow row = PériodesDataGridView.Rows(e.RowIndex) (...)
Partager