Bonsoir,

J'ai un datagridview ou je fais du drag and drop de cellule, tous se passe bien sauf quand je finis de déplacer une cellule et que je clique sur un autre cellule je dois cliquer deux fois pour que cette dernière prenne le focus

J'ai trouvé une solution mais qui ne me satisfait pas pour remédier à ça :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11

Private Sub DataGridView1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles data_grid_carnet.MouseHover

        If bool_focus = True Then

            data_grid_carnet.CurrentCell = data_grid_carnet.Item(int_col_dest, int_ligne_dest)
            data_grid_carnet.Item(int_col_dest, int_ligne_dest).Selected = True
            MsgBox("ok")
            bool_focus = False
        End If
Je place une messagebox et grace à ca il me suffit d'un clic pour donner le focus a une autre cellule. Si je retire la messagebox je dois cliqué deux fois dans une cellule pour lui donné le focus.

Je voudrais trouvé le moyen de faire la même chose mais sans la messagebox

Voici le code complet il suffit de mettre un datagridview sur la form

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
 
Public Class Form1
    Dim dt As DataTable
 
    Dim int_ligne_selec As Integer '//La ligne sélectionnée au début
    Dim int_col_selec As Integer '//La colonne sélectionnée au début
 
    Dim bool_focus As Boolean = False
    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
 
 
    End Sub
 
    Private Sub data_grid_carnet_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data_grid_carnet.CellDoubleClick
 
    End Sub
    ''' <summary>
    ''' EVENEMENT QUAND ON ENFONCE LA SOURIS DANS LA CELLULE
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles data_grid_carnet.MouseDown
 
        int_ligne_selec = data_grid_carnet.HitTest(e.X, e.Y).RowIndex
        int_col_selec = data_grid_carnet.HitTest(e.X, e.Y).ColumnIndex
        If Me.data_grid_carnet.Rows(int_ligne_selec).Cells(int_col_selec).Value.ToString <> vbNullString Then
            int_col_exp = Me.data_grid_carnet.CurrentCell.ColumnIndex '//Récupérer l'index de la colonne cliquée
 
            If int_ligne_selec >= 0 And int_col_selec Then
                data_grid_carnet.DoDragDrop(int_ligne_selec, DragDropEffects.Move)
            End If
        End If
    End Sub
    ''' <summary>
    ''' EVENEMENT AU COURS D'UNE OPERATION DE GLISSER-DEPOSSER
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles data_grid_carnet.DragOver
        Dim p As Point = data_grid_carnet.PointToClient(New Point(e.X, e.Y))
 
        Dim dgt As DataGridViewHitTestType = data_grid_carnet.HitTest(p.X, p.Y).Type
 
        If FONC_VERIF_DIM_DTG(p.X, p.Y) Then
            e.Effect = DragDropEffects.Move
 
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
    ''' <summary>
    ''' EVENEMENT QUAND ON GLISSE UN CONTROLE SUR UN OBJET ET EN RELACHANT LA SOURIS
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles data_grid_carnet.DragDrop
        Try
 
            'Pour stocker l'index de la ligne
            Dim int_ligne_selec As Integer = Convert.ToInt32(e.Data.GetData(Type.GetType("System.Int32")))
 
            'Coordonnée de la cellule quand on relache la souris
            Dim point_cellule As Point = data_grid_carnet.PointToClient(New Point(e.X, e.Y))
            'Dim int_ligne_dest As Integer
            Dim str_cell As String '//Contient la valeur de la cellule sélectionnée
            'Dim int_col_dest As Integer
            Dim str_valeur_nouv_cell As String = String.Empty
            Dim dr As DataRow
 
            '//Récupère la donnée de la cellule
            str_cell = data_grid_carnet.Rows(int_ligne_selec).Cells(int_col_exp).Value.ToString
 
            If str_cell <> vbNullString Then
                '//Coordonnée de la cellule
                int_ligne_dest = data_grid_carnet.HitTest(point_cellule.X, point_cellule.Y).RowIndex
                int_col_dest = data_grid_carnet.HitTest(point_cellule.X, point_cellule.Y).ColumnIndex
                If int_ligne_dest <> 7 Then
                    If int_col_dest <> 0 Then
                        '//Regarde si il ya deja une donnée dans la cellule.
                        str_valeur_nouv_cell = dt.Rows(int_ligne_dest).Item(int_col_dest).ToString
                        If str_valeur_nouv_cell = vbNullString Then
                            '//Effacement du contenu de l'ancienne cellule
                            dt.Rows(int_ligne_selec).Item(int_col_selec) = ""
                            '//Affichage du contenu dans la nouvelle cellule
 
                            dr = dt.Rows(int_ligne_dest)
                            dr(int_col_dest) = str_cell
                            data_grid_carnet.Item(int_col_selec, int_ligne_selec).Selected = False
                            bool_focus = True
                        End If
                    End If
                End If
                '//On efface le contenu
                str_cell = ""
            End If
 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Function FONC_VERIF_DIM_DTG(ByVal x As Integer, ByVal y As Integer) As Boolean
 
        Dim dgt As DataGridViewHitTestType = data_grid_carnet.HitTest(x, y).Type
 
        Return (dgt = DataGridViewHitTestType.Cell OrElse dgt = DataGridViewHitTestType.RowHeader)
 
    End Function
 
    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'DataGridView1.Item(int_col_selec, int_ligne_selec).Selected = False
        'DataGridView1.Item(int_col_dest, 3).Selected = True
        data_grid_carnet.CurrentCell = data_grid_carnet.Item(int_col_dest, int_ligne_dest)
    End Sub
 
    Private Sub DataGridView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
 
    End Sub
 
    Private Sub DataGridView1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles data_grid_carnet.MouseHover
 
        If bool_focus = True Then
 
            data_grid_carnet.CurrentCell = data_grid_carnet.Item(int_col_dest, int_ligne_dest)
            data_grid_carnet.Item(int_col_dest, int_ligne_dest).Selected = True
            MsgBox("ok")
            bool_focus = False
        End If
    End Sub
 
End Class

Si quelle qu'un a une idée
Merci