Combobox dans le datagridview

Fermé
Jirehpascal - Modifié le 18 mai 2018 à 21:26
 Utilisateur anonyme - 18 mai 2018 à 23:03
Bonjour,
je suis entrain de programmer une application en vb 12 et j'utilise un datagridview dont à chaque fois que je clic sur datagridview, le combo apparaît maintenant le problème est que si je me deplace, j'aimerais que la valeur saisie dans le combo reste dans l'index de mon datagridview.

voici le code :

Public Class index
    Dim SelCol As Double
    Dim SelRow As Double
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim rect As Rectangle
        SelRow = DataGridView1.CurrentCell.RowIndex
        SelCol = DataGridView1.CurrentCell.ColumnIndex
        ComboBox1.Width = DataGridView1.CurrentCell.Size.Width

        rect = DataGridView1.GetCellDisplayRectangle(SelCol, SelRow, False)
        ComboBox1.Left = rect.X + DataGridView1.Left
        ComboBox1.Top = rect.Y + DataGridView1.Top
        ComboBox1.Text = DataGridView1.Item(DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex).Value
        ComboBox1.Focus()
        T1.Text = SelCol
        T2.Text = SelRow
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.RowCount = 5
  

    End Sub

    Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
        If e.KeyCode = "40" Then ' BAS
            If DataGridView1.RowCount - 1 = SelRow Then
                DataGridView1.Rows.Add()
            End If
            Dim rect As Rectangle
            SelRow = SelRow + 1
            ComboBox1.Width = DataGridView1.CurrentCell.Size.Width
            rect = DataGridView1.GetCellDisplayRectangle(SelCol, SelRow, False)
            ComboBox1.Left = rect.X + DataGridView1.Left
            ComboBox1.Top = rect.Y + DataGridView1.Top
            ComboBox1.Text = DataGridView1.Item(DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex + 1).Value
            ComboBox1.Focus()

        End If
        If e.KeyCode = "38" Then ' HAUT
            If SelRow = 0 Then
            Else
                Dim rect As Rectangle
                SelRow = SelRow - 1
                ComboBox1.Width = DataGridView1.CurrentCell.Size.Width
                rect = DataGridView1.GetCellDisplayRectangle(SelCol, SelRow, False)
                ComboBox1.Left = rect.X + DataGridView1.Left
                ComboBox1.Top = rect.Y + DataGridView1.Top
                ComboBox1.Text = DataGridView1.Item(DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex + 1).Value
                ComboBox1.Focus()
            End If
        End If

        If e.KeyCode = "39" Then ' DROITE
            If SelCol = 3 Then
                SelCol = -1
                SelRow = SelRow + 1
            End If
            Dim rect As Rectangle
            If DataGridView1.RowCount = SelRow Then
                DataGridView1.Rows.Add()
            End If
            SelCol = SelCol + 1
            ComboBox1.Width = DataGridView1.CurrentCell.Size.Width
            rect = DataGridView1.GetCellDisplayRectangle(SelCol, SelRow, False)
            ComboBox1.Left = rect.X + DataGridView1.Left
            ComboBox1.Top = rect.Y + DataGridView1.Top
            ComboBox1.Text = DataGridView1.Item(DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex + 1).Value
            ComboBox1.Focus()
        End If
        If e.KeyCode = "37" Then ' Gauche
            If SelCol = 0 And SelRow = 0 Then

            Else
                If SelCol = 0 Then
                    SelCol = 4
                    SelRow = SelRow - 1
                End If
                Dim rect As Rectangle
                SelCol = SelCol - 1
                ComboBox1.Width = DataGridView1.CurrentCell.Size.Width
                rect = DataGridView1.GetCellDisplayRectangle(SelCol, SelRow, False)
                ComboBox1.Left = rect.X + DataGridView1.Left
                ComboBox1.Top = rect.Y + DataGridView1.Top
                ComboBox1.Text = DataGridView1.Item(DataGridView1.CurrentCell.ColumnIndex, DataGridView1.CurrentCell.RowIndex + 1).Value
                ComboBox1.Focus()
            End If
        End If
    End Sub

1 réponse

Utilisateur anonyme
18 mai 2018 à 23:03
Bonjour,

pourquoi ne pas utiliser une colonne avec combobox?
https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.forms.datagridviewcomboboxcolumn?redirectedfrom=MSDN&view=netframework-4.8

Et merci à Baladur d'avoir édité ton message pour rendre ton code lisible.
Pour tes prochains posts, voir ici comment ajouter les balises de codes
https://codes-sources.commentcamarche.net/faq/10686-le-nouveau-codes-sources-comment-ca-marche#balises-code
Force bien la couleur à basic
0