Bonjour,

J'ai un menu ContextMenuStrip qui s"affiche lorsque l'utilisateur fais un clic droit sur une cellule du datagridview.

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
 
Public Class Form1
    Dim dt As DataTable
 
    Dim int_col_exp As Integer '//Index de la colonne expédition
    Dim int_col_dest As Integer '//Index de la colonne destination
    Dim int_ligne_dest As Integer
 
    Private Sub frmDataGrid_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillDataInGrids()
        data_grid_carnet.CurrentCell = data_grid_carnet.Item(1, 0)
    End Sub
 
    Private Sub FillDataInGrids()
        dt = New DataTable
 
        'Titre des colonnes du datagrid1
        dt.Columns.Add("Name", Type.GetType("System.String"))
        dt.Columns.Add("Designation", Type.GetType("System.String"))
        dt.Columns.Add("Department", Type.GetType("System.String"))
        dt.Columns.Add("Salary", Type.GetType("System.String"))
 
        'Ligne enregistrement
        Dim Dr As DataRow
        'les enregistrement du datagrid1
        Dr = dt.NewRow
        Dr("Name") = "Tom"
        Dr("Designation") = "Developer"
        Dr("Department") = "Engg"
        Dr("Salary") = "1000"
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        Dr("Name") = "Jerry"
        Dr("Designation") = "Developer"
        Dr("Department") = "Engg"
        Dr("Salary") = "1000"
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        Dr("Name") = "Micky"
        Dr("Designation") = "Analyst"
        Dr("Department") = "Engg"
        Dr("Salary") = "2000"
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        dt.Rows.Add(Dr)
 
        Dr = dt.NewRow
        dt.Rows.Add(Dr)
 
        'Now Bind the DataGrids to these table
        data_grid_carnet.DataSource = dt
 
        Dim _contextmenu As New ContextMenuStrip
        _contextmenu.Items.Add("Menu1")
        _contextmenu.Items.Add("Menu2")
        _contextmenu.Items.Add("Menu3")
        AddHandler _contextmenu.ItemClicked, AddressOf contextmenu_click
        For Each rw As DataGridViewRow In data_grid_carnet.Rows
            For Each c As DataGridViewCell In rw.Cells
                c.ContextMenuStrip = _contextmenu
            Next
        Next
 
    End Sub
 
    Private Sub contextmenu_click(ByVal sender As System.Object, _
                                  ByVal e As ToolStripItemClickedEventArgs)
        Dim clickCell As DataGridViewCell = data_grid_carnet.SelectedCells(0)
        data_grid_carnet.Rows(Me.data_grid_carnet.CurrentCell.RowIndex).Selected = True
        Select Case e.ClickedItem.Text
            Case "Menu1"
                MsgBox("Menu1")
            Case "Menu2"
                MsgBox("Menu2")
            Case "Menu3"
                MsgBox("Menu3")
        End Select
        MsgBox(Me.data_grid_carnet.CurrentCell.RowIndex)
    End Sub
    Private Sub data_grid_carnet_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles data_grid_carnet.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If e.ColumnIndex >= 0 And e.RowIndex >= 0 Then
                data_grid_carnet.CurrentCell = data_grid_carnet.Rows(e.RowIndex).Cells(e.ColumnIndex)
            End If
        End If
    End Sub
End Class
J'aimerais ajouter des sous menus a mon ContextMenuStrip mais je ne vois pas comment faire car je ne passe pas par le contrôle graphique.

exemple:
Menu1
Menu2
Sousmenu1
Sousmenu2
Menu3

J'aimerais aussi savoir si la façon dont je procède pour afficher un menu sur un Datagridview est correct ou existe-t-il d autre solution ??

Auriez vous de la doc ou des exemples ?

D'avance merci