none
(Visual Studio 2008) Intercepter la saisie dans un DataGridView RRS feed

  • Discussion générale

  • Bonjour,

    J'ai eu bien du mal à trouver le code pour intercepter les touches dans une cellule en saisie dans une grille

    Je vous livre ma solution.

    Une datagridview de 4 colonnes, JOUET, QTE, PRIX, MONTANT, avec la colonne QTE en saisie, et EDITMODE = EDITONENTER
    Quand je saisie une QTE, j'affiche le montant.
    Si on efface la QTE, j'efface le montant
    Voici le code

    Public Class Form1
        Dim wNombre As Integer = 0
        Dim cnt As DataGridViewTextBoxEditingControl : Dim grid As DataGridView
    
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            With Me.dgvRepasPetDej.Rows
                .Clear()
            End With
            With Me.dgvRepasPetDej.Rows
                .Add("Jouet Fille", _
                    "", _
                    18.9, _
                    "")
                .Add("Jouet Garçon", _
                    "", _
                    12.34, _
                    "")
            End With
            Me.dgvRepasPetDej.Rows(0).Cells(1).Style.BackColor = Color.AliceBlue : Me.dgvRepasPetDej.Rows(1).Cells(1).Style.BackColor = Color.AliceBlue
            dgvRepasPetDej.Select()
            dgvRepasPetDej.Rows(0).Cells(1).Selected = True
            dgvRepasPetDej.BeginEdit(True)
            dgvRepasPetDej.ClearSelection()
        End Sub
    
        Private Sub dgvRepasPetDej_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvRepasPetDej.CellEndEdit
            rowIndex = dgvRepasPetDej.CurrentCell.RowIndex
            colIndex = dgvRepasPetDej.CurrentCell.ColumnIndex
            MessageBox.Show("CellEndEdit - rowIndex = " & rowIndex & "  colIndex = " & colIndex & vbCrLf & "sender " & sender.name)
    
            If colIndex <> 1 Then Exit Sub
            If dgvRepasPetDej.Item(1, rowIndex).Value = Nothing Then
                dgvRepasPetDej.Item(1, rowIndex).Value = "" : dgvRepasPetDej.Item(3, rowIndex).Value = ""
            End If
        End Sub
    
        Private Sub dgvRepasPetDej_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvRepasPetDej.EditingControlShowing
            rowIndex = dgvRepasPetDej.CurrentCell.RowIndex
            colIndex = dgvRepasPetDej.CurrentCell.ColumnIndex
            MessageBox.Show("EditingControlShowing - rowIndex = " & rowIndex & "  colIndex = " & colIndex & vbCrLf & "sender " & sender.name)
    
            grid = CType(sender, DataGridView)
            cnt = CType(grid.EditingControl, DataGridViewTextBoxEditingControl)
            If TypeOf grid.EditingControl Is DataGridViewTextBoxEditingControl Then
                RemoveHandler cnt.KeyDown, AddressOf EditControlKeyPress
                AddHandler cnt.KeyDown, AddressOf EditControlKeyPress
            Else
                RemoveHandler cnt.KeyDown, AddressOf EditControlKeyPress
            End If
        End Sub
        Private Sub EditControlKeyPress(ByVal sender As Object, ByVal e As KeyEventArgs)
            'MessageBox.Show("EditControlKeyPress - e.KeyCode = " & e.KeyCode)
            Dim wPrix As Decimal = 0 : Dim wMontant As Decimal = 0
            If (e.KeyCode = Keys.Delete) Or (e.KeyCode = Keys.Escape) Or (e.KeyCode = Keys.Back) Then
                If dgvRepasPetDej.Rows.Count < 1 Then Exit Sub
                rowIndex = dgvRepasPetDej.CurrentCell.RowIndex
                colIndex = dgvRepasPetDej.CurrentCell.ColumnIndex
                If colIndex <> 1 Then Exit Sub
                dgvRepasPetDej.Item(1, rowIndex).Value = 0
                wNombre = 0
                dgvRepasPetDej.Item(1, rowIndex).Value = "" : dgvRepasPetDej.Item(3, rowIndex).Value = ""
            Else
                If (e.KeyCode > 47) And (e.KeyCode < 58) Or _
                   (e.KeyCode > 95) And (e.KeyCode < 106) Then
                    If dgvRepasPetDej.Rows.Count < 1 Then Exit Sub
                    rowIndex = dgvRepasPetDej.CurrentCell.RowIndex
                    colIndex = dgvRepasPetDej.CurrentCell.ColumnIndex
                    If colIndex <> 1 Then Exit Sub
                    wNombre = CovertKeyCode(e.KeyCode)
                    wPrix = dgvRepasPetDej.Item(2, rowIndex).Value
                    wMontant = wNombre * wPrix
                    dgvRepasPetDej.Item(3, rowIndex).Value = Format(wMontant, "0.00")
                Else
                    Beep() : Beep()
                    MessageBox.Show("e.KeyCode = " & e.KeyCode)
                    e.Handled = True
                    Exit Sub
                End If
            End If
        End Sub
    
    End Class


    Cordialement SC

    lundi 24 juin 2013 05:38

Toutes les réponses

  • Bonjour,

    Merci M. CONSALVI d'avoir partagé votre solution à la communauté.

    Cordialement


    Gilles TOURREAU - MVP C#
    Architecte logiciel/Consultant/Formateur Freelance
    Blog : http://gilles.tourreau.fr
    - MCPD : Enterprise Developper / Windows Developper 3.5 / ASP .NET 3.5/4.0
    - MCITP : SQL Server 2008 Developper
    - MCTS : ADO .NET 3.5 / SQL Server 2008 Developper / Windows Forms 3.5 / ASP .NET 3.5/4.0

    lundi 24 juin 2013 22:36
    Modérateur