je veux faire une selection multiple de lignes d'une datagrid pour les supprimer.comment faire
Version imprimable
je veux faire une selection multiple de lignes d'une datagrid pour les supprimer.comment faire
Voilà le code pour sélectionner plusieurs lignes dans un datagrid
Pour ce qui est de supprimer ces lignes, il y a la méthode RemoveItem...Code:
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 option explicit Enum ModifierKeysEnum mkNone = 0 mkShift = 1 mkControl = 2 End Enum Private ModifierKey As ModifierKeysEnum Private Sub grdLevel1_Click() Dim CurrentClickedRow As Long Dim i As Long With grdLevel1 CurrentClickedRow = .MouseRow If CurrentClickedRow = 0 Then Exit Sub If CurrentClickedRow = .Rows - 1 Then Exit Sub .Visible = False Select Case ModifierKey Case mkNone 'Clear all highlights and highlight this row Screen.MousePointer = vbHourglass .Col = 1 For i = 1 To .Rows - 2 .Row = i If .CellBackColor = vbDarkBlue Then HighlightRow grdLevel1, hsOff End If Next i .Row = CurrentClickedRow: HighlightRow grdLevel1, hsOn Screen.MousePointer = vbDefault Case mkShift 'Highlight all rows between this row and mLastRow .Row = CurrentClickedRow If mLastRow = CurrentClickedRow Or mLastRow = -1 Then HighlightRow grdLevel1, hsOn ElseIf mLastRow > CurrentClickedRow Then For i = CurrentClickedRow To mLastRow .Row = i: HighlightRow grdLevel1, hsOn Next i Else For i = mLastRow To CurrentClickedRow .Row = i: HighlightRow grdLevel1, hsOn Next i End If Case mkControl 'Toggle highlight state of clicked row HighlightRow grdLevel1, IIf(.CellBackColor = vbDarkBlue, hsOff, hsOn) End Select mLastRow = CurrentClickedRow .Visible = True End With 'grdLevel1 Screen.MousePointer = vbDefault End Sub '----------------- Public Sub HighlightRow(aGrid As MSFlexGrid, NewState As HighlightState) Dim i As Integer With aGrid For i = 0 To .Cols - 1 .Col = i .CellBackColor = IIf(NewState = hsOff, vbWhite, vbDarkBlue) .CellForeColor = IIf(NewState = hsOff, vbBlack, vbWhite) Next i End With 'aGrid End Sub
merci pour ta reponse meme si ce code ne marche pas.
j'ai essayé le code suivant mais il aussi ne donne pas le resultat souhaité
Code:
1
2
3
4
5 Dim bm As Variant For Each bm In grdLevel1.SelBookmarks Me.grdLevel1.AllowDelete = True grdLevel1.RowBookmark(bm).Remove Next
Qu'est-ce qui ne fonctionne pas? La sélection multiple ou la suppression de ligne? Je te conseille de mettre des lignes de debug dans ton code. tu dois t'assurer que la collection grdLevel1.SelBookmarks est bien remplie...
Il semblerait qu'il ait une confusion enre MsFlexGrid et DataGrid.
Les fonctinnalités de selection/suppression données par Anneca sont pour un MsFlexGrig, je pense.
Personellement j'utilise dao avec le databound grid, et je ne connais pas le DataGrid. Désolé de ne pas pouvoir aider plus.