IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VB.NET Discussion :

Checkbox en DataGridView état Checked (True)


Sujet :

VB.NET

  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut Checkbox en DataGridView état Checked (True)
    Bonjour,
    J'ai un DataGridView avec en colonne de type checkbox. Je souhaiterai que lorsque je coche le checkbox
    des cells reviens reviens 0.00

    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
     Private Sub DView_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DView.CellClick
     
    For Each row As DataGridViewRow In DView.Rows
                Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("checkBoxColumn").Value)
                If isSelected Then
                    For i As Integer = 0 To Me.DView.Rows.Count - 1
                        Dim total As Integer = 0
                        Me.DView(5, i).Value = Format(Me.DView(5, i).Value * total, "0.00")
                        Me.DView(7, i).Value = Format(Me.DView(7, i).Value * total, "0.00")
                        Me.DView(8, i).Value = Format(Me.DView(8, i).Value * total, "0.00")
                        Me.DView(9, i).Value = Format(Me.DView(9, i).Value * total, "0.00")
                        Me.DView(10, i).Value = Format(Me.DView(10, i).Value * total, "0.00")
                        Me.DView(11, i).Value = Format(Me.DView(11, i).Value * total, "0.00")
                        Me.DView(12, i).Value = Format(Me.DView(12, i).Value * total, "0.00")
                        Me.DView(12, i).Value = Format(Me.DView(12, i).Value * total, "0.00")
                        Me.DView(13, i).Value = Format(Me.DView(13, i).Value * total, "0.00")
                        Me.DView(14, i).Value = Format(Me.DView(14, i).Value * total, "0.00")
                        Me.DView(15, i).Value = Format(Me.DView(15, i).Value * total, "0.00")
                        Me.DView(16, i).Value = Format(Me.DView(16, i).Value * total, "0.00")
                    Next
     
                End If
            Next
        End Sub
    Merci beaucoup et bonne journée.

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour

    code exemple .vb:
    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
     
    Public Class Form2
        Private Produits As DataTable
        Private rnd As New Random
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Produits = GetTable()
            DGV.DataSource = Produits
            Me.DGV.Columns("PrixHT").DefaultCellStyle.Format = "F2" 'format virgule fixe 2 decimales
     
        End Sub
        Private Function GetTable() As DataTable
     
            Dim dt As New DataTable("produits")
            dt.Columns.Add("Quantite", GetType(Integer))
            dt.Columns.Add("PrixUnitaire", GetType(Double))
            dt.Columns.Add("PrixHT", GetType(Double))
            dt.Columns.Add("Verifier", GetType(Boolean))
            Dim dr As DataRow = dt.NewRow()
            For n As Integer = 1 To 10
     
                dr(0) = rnd.Next(10, 20)
                dr(1) = Math.Round(rnd.NextDouble() * 100, 2)
                dr(2) = dr(0) * dr(1) 'PrixHT=Quantite*PrixUnitaire
                dr(3) = False
                dt.Rows.Add(dr)
                dr = dt.NewRow()
            Next
            Return dt
        End Function
     
        Private Sub DGV_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
            If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
            Dim row As DataGridViewRow = DGV.Rows(e.RowIndex)
            Dim currRow As Integer = e.RowIndex
            Dim currCol As Integer = e.ColumnIndex
            Dim isSelected As Boolean = row.Cells("Verifier").Selected
            If isSelected Then
                Dim total As Integer = 0
                ClearChecked(total, currCol)
            End If
        End Sub
        Private Sub ClearChecked(tot As Double, col As Integer)
            For irow As Integer = 0 To DGV.Rows.Count - 1
                Me.DGV(col - 1, irow).Value = Format(Me.DGV(col - 1, irow).Value * tot, "F2")
     
            Next
     
        End Sub
    End Class


    bon code..
    ..

  3. #3
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    Bonjour, et merci mabrouki
    dans la cellule précise dans la ligne cocher seulement dans le ligne sélectionnée et pas dans toute la colonne.
    Par exemple cellule N 2 de la ligne cocher.
    J'ai changé le code mais il na pas marché.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     Private Sub ClearChecked(ByVal tot As Double, ByVal col As Integer)
            For irow As Integer = 0 To DView.Rows.Count - 1
                ' Me.DView(col - 1, irow).Value = Format(Me.DView(col - 1, irow).Value * tot, "F2")
                Me.DView(2, irow).Value = Format(Me.DView(2, irow).Value * tot, "f2")
            Next
        End Sub

  4. #4
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Il faudrait savoir d'abord ce que tu veux exactement car le premier code posté "balaie" toutes les lignes du DGV et non la ligne en cours.....

  5. #5
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    bonsoir mabrouki,
    je veux exactement que code du DGV marche avec des cellule précises dans la ligne en cours (la ligne cocher du chekbox)
    par ex dans la ligne sectionne (cheked=true) la valeur du cellule 2, 3 ... devient 0.00 quelque cellules et pas toute la ligne si c'est possible
    merci

  6. #6
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    1/ ce code au début abrège les "souffrances" concernant le format des cellules:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
       Me.DGV.Columns("PrixHT").DefaultCellStyle.Format = "F2" 'format virgule fixe 2 decimales
            Me.DGV.Columns("PrixTVA").DefaultCellStyle.Format = "F2" 'format virgule fixe 2 decimales
    2/ l'indexation des Cells cibles par le nom des colonnes fait le même boulot.
    3/ dans le cas que tu cherches à résoudre ,e.RowIndex (ligne en cours) est ton ami.

    code .vb revu qui fera ton bonheur:
    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
     
    Public Class Form3
        Private Produits As DataTable
        Private rnd As New Random
        Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Produits = GetTable()
            DGV.DataSource = Produits
            Me.DGV.Columns("PrixHT").DefaultCellStyle.Format = "F2" 'format virgule fixe 2 decimales
            Me.DGV.Columns("PrixTVA").DefaultCellStyle.Format = "F2" 'format virgule fixe 2 decimales
     
        End Sub
        Private Function GetTable() As DataTable
     
            Dim dt As New DataTable("produits")
            dt.Columns.Add("Quantite", GetType(Integer))
            dt.Columns.Add("PrixUnitaire", GetType(Double))
            dt.Columns.Add("PrixHT", GetType(Double))
            dt.Columns.Add("TauxTVA", GetType(Double))
            dt.Columns.Add("PrixTVA", GetType(Double))
            dt.Columns.Add("Verifier", GetType(Boolean))
            Dim dr As DataRow = dt.NewRow()
            Dim tauxTVA As Double = 15 'pourcentage
            For n As Integer = 1 To 10
     
                dr("Quantite") = rnd.Next(10, 20)
                dr("PrixUnitaire") = Math.Round(rnd.NextDouble() * 100, 2)
                dr("PrixHT") = dr("Quantite") * dr("PrixUnitaire") 'PrixHT=Quantite*PrixUnitaire
                dr("TauxTVA") = tauxTVA
                dr("PrixTVA") = Math.Round(dr("PrixHT") + dr("PrixHT") * dr("TauxTVA") / 100.0, 2) 'PrixTVA=PrixHT+PrixHT*TauxTVA/100.0
                dr("Verifier") = False
     
                dt.Rows.Add(dr)
                dr = dt.NewRow()
            Next
            Return dt
        End Function
     
        Private Sub DGV_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
            If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
            Dim row As DataGridViewRow = DGV.Rows(e.RowIndex)
            Dim isSelected As Boolean = row.Cells("Verifier").Selected
            If isSelected Then
                Dim total As Integer = 0
                ClearChecked(total, e.RowIndex)
            End If
        End Sub
        Private Sub ClearChecked(tot As Double, irow As Integer)
            Me.DGV.Rows(irow).Cells("PrixHT").Value = 0.0
            Me.DGV.Rows(irow).Cells("PrixTVA").Value = 0.0
     
     
        End Sub
    End Class

    bon code....

  7. #7
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    merci mabrouki pour ta réponse rapide,
    et pour le complément d'informations
    ce code marche très bien

    est ce que en peut appliquer la meme chose d'un table lie d'une base de donne access

  8. #8
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    le datagridview peut afficher tout type de DataTable quel que soit la BD.

  9. #9
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    j'arrive pas à appliquer le code avec une datagridview lie avec une bd
    et merci bq mabrouki

  10. #10
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    j'ai résolu le problème
    j'ai seulement ajouter un checkbox a ma datagridview lie A une table

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Dim ColumncheckBox As New DataGridViewCheckBoxColumn()
            ColumncheckBox.HeaderText = "Etat"
            ColumncheckBox.Width = 30
            ColumncheckBox.Name = "ColumncheckBox"
            DView.Columns.Insert(18, ColumncheckBox)
    merci beaucoup MABROUKI pour ton aide.

  11. #11
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Citation Envoyé par ahmedfa71 Voir le message
    j'ai résolu le problème
    j'ai seulement ajouter un checkbox a ma datagridview lie A une table

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Dim ColumncheckBox As New DataGridViewCheckBoxColumn()
            ColumncheckBox.HeaderText = "Etat"
            ColumncheckBox.Width = 30
            ColumncheckBox.Name = "ColumncheckBox"
            DView.Columns.Insert(18, ColumncheckBox)
    merci beaucoup MABROUKI pour ton aide.
    ATTENTION:
    l' event CellClick est inefficient pour ce que tu cherches à obtenir .
    Voici ce qu'en dit la doc MSDN Lib Fr alas:
    Si vous souhaitez répondre immédiatement au clic des utilisateurs sur une cellule de case à cocher, vous pouvez gérer l'événement DataGridView.CellClick, mais cet événement se produit avant la mise à jour de la valeur de la cellule. (Si vous avez besoin de la nouvelle valeur lors du clic, une option consiste à calculer la valeur attendue d'après la valeur actuelle mais c'est du blablah). Une autre approche consiste à valider immédiatement la modification et à gérer l'événement DataGridView.CellValueChanged pour y répondre. Pour valider la modification lorsque l'utilisateur clique sur la cellule, vous devez gérer l'événement DataGridView.CurrentCellDirtyStateChanged. Dans le gestionnaire, si la cellule active est une cellule de case à cocher, appelez la méthode DataGridView.CommitEdit et passez la valeur Commit.

    C'est donc l'event CellValueChanged qui il faut utiliser (au lieu de CellClick) dans le code exemple deja communiqué couplé à l'event CurrentCellDirtyStateChanged.
    Le code deja communiqué est donc à revoir comme suit:
    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
     
     
     ' A virer
     ' Sub DGV_CellClick(sender As Object, e As 'System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellClick
            'event inefficient
     '   End Sub
     
     ' code de substitution 
     Private Sub DGV_CurrentCellDirtyStateChanged(sender As Object, e As System.EventArgs) Handles DGV.CurrentCellDirtyStateChanged
            If TypeOf Me.DGV.CurrentCell Is DataGridViewCheckBoxCell Then
                Me.DGV.CommitEdit(DataGridViewDataErrorContexts.Commit)
            End If
        End Sub
    Private Sub DGV_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellValueChanged
     
            If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
            Dim row As DataGridViewRow = DGV.Rows(e.RowIndex)
     
            Dim chk As DataGridViewCheckBoxCell = row.Cells("Verifier")
            Dim isSelected As Boolean = chk.Value
            If isSelected Then
                Dim total As Integer = 0
                ClearColumnChecked(total, e.RowIndex)
     
            End If
        End Sub
        Private Sub ClearColumnChecked(tot As Double, irow As Integer)
            Me.DGV.Rows(irow).Cells("PrixHT").Value = tot
            Me.DGV.Rows(irow).Cells("PrixTVA").Value = tot
     
        End Sub
    bon code...

  12. #12
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2012
    Messages
    36
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2012
    Messages : 36
    Par défaut
    bonjour MABROUKI,
    j'ai changer comme tu l'a dit
    merci beaucoup.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 7
    Dernier message: 08/04/2020, 15h26
  2. [VB.NET] Checkbox dans DataGridView
    Par horzy dans le forum Windows Forms
    Réponses: 6
    Dernier message: 09/02/2009, 16h24
  3. CheckBox avec 3 états
    Par Julius_Dev dans le forum AWT/Swing
    Réponses: 7
    Dernier message: 22/11/2006, 17h39
  4. [C#] CheckBox et différents états coché
    Par Neitsa dans le forum Windows Forms
    Réponses: 2
    Dernier message: 15/05/2006, 00h01
  5. Réponses: 7
    Dernier message: 08/03/2006, 16h15

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo