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

Windows Forms Discussion :

Problème pour ajouter une colonne checkbox dans mon datagridview


Sujet :

Windows Forms

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut Problème pour ajouter une colonne checkbox dans mon datagridview
    Bonjour,

    Je voudrais ajouter une colonne avec une checkbox dans mon datagridview en 4 ème position.

    Donc pour ajouter une colonne avec checkbox

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
            Dim colToCheck As New DataGridViewCheckBoxColumn
            DataGridPoste.Columns.Add(colToCheck)
    Pour le moment j'ai :

    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
     
     Me.DataGridPoste.ColumnCount = 7
            Me.DataGridPoste.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
            Me.DataGridPoste.Columns(0).Name = "Poste vacant"
            Me.DataGridPoste.Columns(0).Width = 300
            Me.DataGridPoste.Columns(1).Name = "CV"
            Me.DataGridPoste.Columns(1).Width = 80
            Me.DataGridPoste.Columns(2).Name = "Lettre"
            Me.DataGridPoste.Columns(2).Width = 80
            Me.DataGridPoste.Columns(3).Name = "Divers"
            Me.DataGridPoste.Columns(3).Width = 80
             Me.DataGridPoste.Columns(4).Name = "Interne"
            Me.DataGridPoste.Columns(4).Width = 70
            Me.DataGridPoste.Columns(5).Name = "Politique"
            Me.DataGridPoste.Columns(5).Width = 70
            Me.DataGridPoste.Columns(6).Name = "Spontanée"
            Me.DataGridPoste.Columns(6).Width = 70
    Comment faire pour transformer ma colonne 4 en colonne checkbox par programmation ???

    Bon j'ai fait ainsi:

    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
     
            Me.DataGridPoste.ColumnCount = 4
            Me.DataGridPoste.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
            Me.DataGridPoste.Columns(0).Name = "Poste vacant"
            Me.DataGridPoste.Columns(0).Width = 300
            Me.DataGridPoste.Columns(1).Name = "CV"
            Me.DataGridPoste.Columns(1).Width = 80
            Me.DataGridPoste.Columns(2).Name = "Lettre"
            Me.DataGridPoste.Columns(2).Width = 80
            Me.DataGridPoste.Columns(3).Name = "Divers"
            Me.DataGridPoste.Columns(3).Width = 80
            Dim col4ToCheck As New DataGridViewCheckBoxColumn
            Dim col5ToCheck As New DataGridViewCheckBoxColumn
            Dim col6ToCheck As New DataGridViewCheckBoxColumn
            col4ToCheck.Width = 70
            col4ToCheck.HeaderText = "Interne"
            col5ToCheck.Width = 70
            col5ToCheck.HeaderText = "Politique"
            col6ToCheck.Width = 70
            col6ToCheck.HeaderText = "Spontanée"
            DataGridPoste.Columns.Insert(4, col4ToCheck)
            DataGridPoste.Columns.Insert(5, col5ToCheck)
            DataGridPoste.Columns.Insert(6, col6ToCheck)
    Mais quand je clic dedans rien ne se passe(pas de petit v ==> vert(peut etre faut il convertir en boolean ??))

    D'avance merci

  2. #2
    Nouveau membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Juin 2011
    Messages
    46
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2011
    Messages : 46
    Points : 34
    Points
    34
    Par défaut
    Bonjour,

    Tu pourrais passer par le .design et ensuite récupérer le code qui t'intéresse, au moins, tu es sur que ce sera bon.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
        Private Sub InitializeComponent()
            '...'
            Me.Column2 = New System.Windows.Forms.DataGridViewCheckBoxColumn
     
            Me.Column2.HeaderText = "ColonneCheckBox"
            Me.Column2.Name = "ColonneCheckBox"
            '...'
        End Sub
     
        Friend WithEvents Column2 As System.Windows.Forms.DataGridViewCheckBoxColumn
    Chez moi, ça marche, je peux cocher et décocher, ça me créé même des lignes si je coche la dernière ligne.

    D'ailleurs, ton 2ème code marche aussi chez moi.

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    En faite mon Datagridview est en readonly.

    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
     
    '//Charge le datagridview(Postes pour lesquels le candidat postule) 
            Me.DataGridPoste.ColumnCount = 4
            Me.DataGridPoste.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
            Me.DataGridPoste.Columns(0).Name = "Poste vacant"
            Me.DataGridPoste.Columns(0).Width = 300
            Me.DataGridPoste.Columns(0).ReadOnly = True
            Me.DataGridPoste.Columns(1).Name = "CV"
            Me.DataGridPoste.Columns(1).Width = 80
            Me.DataGridPoste.Columns(1).ReadOnly = True
            Me.DataGridPoste.Columns(2).Name = "Lettre"
            Me.DataGridPoste.Columns(2).Width = 80
            Me.DataGridPoste.Columns(2).ReadOnly = True
            Me.DataGridPoste.Columns(3).Name = "Divers"
            Me.DataGridPoste.Columns(3).Width = 80
            Me.DataGridPoste.Columns(3).ReadOnly = True
            Dim col4ToCheck As New DataGridViewCheckBoxColumn
            Dim col5ToCheck As New DataGridViewCheckBoxColumn
            Dim col6ToCheck As New DataGridViewCheckBoxColumn
            col4ToCheck.Width = 70
            col4ToCheck.HeaderText = "Interne"
            col5ToCheck.Width = 70
            col5ToCheck.HeaderText = "Politique"
            col6ToCheck.Width = 70
            col6ToCheck.HeaderText = "Spontanée"
            DataGridPoste.Columns.Insert(4, col4ToCheck)
            DataGridPoste.Columns.Insert(5, col5ToCheck)
            DataGridPoste.Columns.Insert(6, col6ToCheck)
            col4ToCheck.ReadOnly = False
            col5ToCheck.ReadOnly = False
            col6ToCheck.ReadOnly = False
    Comment faire pour parcourir mon datagridview et détecter si la checkbox est cochée ou non ??

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
            For i = 0 To DataGridPoste.Rows.Count - 1
                  .....
            Next
    D'avance merci

  4. #4
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
            For Each row As DataGridViewRow In DataGridPoste.Rows
     
                MessageBox.Show(CStr(row.Cells(4).Value))
     
            Next
    Merci de ton aide ZoomBox

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    J'ai encore un soucis la dernière checkbox me renvoie toujours false même quand celle-ci est checker !!!!!

  6. #6
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour jacko842
    par code essaye de modifier simplement un tout petit peu ton code car il manque le CellTemplate & Valuetype & etat par defaut unchecked.....
    ton code modifie avec un button qui "fille" le dgv et un 2eme bouton pour verifier l'etat d'une case à cocher et lui affecter une valeur (coche):
    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
     
    Public Class Form1
    'cree et fille le dgv
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.DataGridPoste.ColumnCount = 7
            Me.DataGridPoste.AutoGenerateColumns = False
            Me.DataGridPoste.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
            Me.DataGridPoste.Columns(0).Name = "Poste vacant"
            Me.DataGridPoste.Columns(0).Width = 300
            Me.DataGridPoste.Columns(1).Name = "CV"
            Me.DataGridPoste.Columns(1).Width = 80
            Me.DataGridPoste.Columns(2).Name = "Lettre"
            Me.DataGridPoste.Columns(2).Width = 80
            Me.DataGridPoste.Columns(3).Name = "Divers"
            Me.DataGridPoste.Columns(3).Width = 80
            '
            'definir CellTemplate & Valuetype & etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(4, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(4).CellTemplate = New DataGridViewCheckBoxCell()
            Me.DataGridPoste.Columns(4).Name = "Interne"
            Me.DataGridPoste.Columns(4).Width = 70
            Me.DataGridPoste.Columns(4).ValueType = GetType(Boolean)
     
            'etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(5, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(5).Name = "Politique"
            Me.DataGridPoste.Columns(5).Width = 70
            Me.DataGridPoste.Columns(5).ValueType = GetType(Boolean)
            'etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(6, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(6).Name = "Spontanée"
            Me.DataGridPoste.Columns(6).Width = 70
            Me.DataGridPoste.Columns(6).ValueType = GetType(Boolean)
            'add 5 rows for test
            Dim myDGVCell As New DataGridViewCheckBoxCell
            For i As Integer = 0 To 4
                Me.DataGridPoste.Rows.Add()
                Me.DataGridPoste.Rows(i).Cells("Poste vacant").Value = "PosteVacant" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("CV").Value = "CV" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("Lettre").Value = "Lettre" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("Divers").Value = "Divers" & (i + 1).ToString
     
            Next
     
     
        End Sub
        'verifie si cellule de column 4 et ligne 3 est checkee ou non 
        'si elle n'est pas checke affiche une alerte et la checke
        'le cast vers un DataGridViewCheckBoxCell est necessaire
        '
        Private Sub btnCheckCell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckCell.Click
            Dim myDGVCell As DataGridViewCheckBoxCell = Me.DataGridPoste.Rows(3).Cells("Interne")
            If myDGVCell IsNot Nothing Then 'si n'est pas checke la checke
                If myDGVCell.Value = True Then
                    MessageBox.Show("is checked")
                Else 'si n'est pas checke la checke
                    MessageBox.Show("is unchecked")
                    myDGVCell.Value = True
                End If
            End If
        End Sub
    End Class
    Sur le designer pour la colonne incrimine tu change la prop ColumnType-attention elle n'existe que sur le designer-
    Bon code.......

  7. #7
    Membre actif
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Points : 244
    Points
    244
    Par défaut
    ça marche pas !!!!

    Il garde toujours la valeurs a false, sauf si je clique sur un autre évènement et que je vérifie seulement après la checkbox (elle sera a TRUE)

    (Problème avec le focus)

    Comment faire ??

  8. #8
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 442
    Points
    4 442
    Par défaut
    bonjour jacko842
    Moi sans voir le code avec lequel tu verifies si la celluke est passee à true ou à false je ne peux pas te repondre.....
    Avec des ca marche ,ca marche pas je ne peux pas repondre ....
    voici le code deja donne repris en initialisant les cellules checkboxes toutes à false....avec un label qui affiche la valeur de la cellule "interne" numero 3
    avant et apres le click sur le bouton btnCheckCell et ou chez moi la valeur passe de false à true.............

    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
     
     
    Public Class Form1
        'cree et "fill" le dgv
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Me.DataGridPoste.ColumnCount = 7
            Me.DataGridPoste.AutoGenerateColumns = False
            Me.DataGridPoste.Font = New Font("Microsoft Sans Serif", 8, FontStyle.Bold)
            Me.DataGridPoste.Columns(0).Name = "Poste vacant"
            Me.DataGridPoste.Columns(0).Width = 300
            Me.DataGridPoste.Columns(1).Name = "CV"
            Me.DataGridPoste.Columns(1).Width = 80
            Me.DataGridPoste.Columns(2).Name = "Lettre"
            Me.DataGridPoste.Columns(2).Width = 80
            Me.DataGridPoste.Columns(3).Name = "Divers"
            Me.DataGridPoste.Columns(3).Width = 80
            '
            'definir CellTemplate & Valuetype & etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(4, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(4).CellTemplate = New DataGridViewCheckBoxCell()
            Me.DataGridPoste.Columns(4).Name = "Interne"
            Me.DataGridPoste.Columns(4).Width = 70
            Me.DataGridPoste.Columns(4).ValueType = GetType(Boolean)
     
            'etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(5, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(5).Name = "Politique"
            Me.DataGridPoste.Columns(5).Width = 70
            Me.DataGridPoste.Columns(5).ValueType = GetType(Boolean)
            'etat par defaut unchecked
            Me.DataGridPoste.Columns.Insert(6, New DataGridViewCheckBoxColumn)
            Me.DataGridPoste.Columns(6).Name = "Spontanée"
            Me.DataGridPoste.Columns(6).Width = 70
            Me.DataGridPoste.Columns(6).ValueType = GetType(Boolean)
            'add 5 rows for test
            Dim myDGVCell As New DataGridViewCheckBoxCell
            For i As Integer = 0 To 4
                Me.DataGridPoste.Rows.Add()
                Me.DataGridPoste.Rows(i).Cells("Poste vacant").Value = "PosteVacant" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("CV").Value = "CV" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("Lettre").Value = "Lettre" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("Divers").Value = "Divers" & (i + 1).ToString
                Me.DataGridPoste.Rows(i).Cells("Interne").Value = False
                Me.DataGridPoste.Rows(i).Cells("Politique").Value = False
                Me.DataGridPoste.Rows(i).Cells("Spontanée").Value = False
            Next
            'valeur de cellue 3 de colonne "interne"
            Me.LabelValeurCellCheck.Text = Me.DataGridPoste.Rows(3).Cells("Interne").Value.ToString
     
     
        End Sub
        'verifie si cellule de column 4 et ligne 3 est checkee ou non 
        'si elle n'est pas checke affiche une alerte et la checke
        'le cast vers un DataGridViewCheckBoxCell est necessaire
        '
        Private Sub btnCheckCell_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckCell.Click
            Dim myDGVCell As DataGridViewCheckBoxCell = Me.DataGridPoste.Rows(3).Cells("Interne")
            If myDGVCell IsNot Nothing Then 'si n'est pas checke la checke
                If myDGVCell.Value = False Then
                    myDGVCell.Value = True
                End If
            End If
            'valeur de cellule 3 de colonne "interne" apres le click
            Me.LabelValeurCellCheck.Text = myDGVCell.Value.ToString
     
        End Sub
    End Class
    Ne mele surtout pas le focus à tes deboires.............
    bon code....

Discussions similaires

  1. [WD16] problème pour ajouter une ligne dans la table
    Par T1929D dans le forum WinDev
    Réponses: 3
    Dernier message: 02/08/2013, 22h39
  2. Réponses: 2
    Dernier message: 19/05/2006, 18h01
  3. Réponses: 4
    Dernier message: 04/02/2006, 17h44
  4. [SQL Server 2000] ajouter une colonne identité dans une vue?
    Par CetTer dans le forum MS SQL Server
    Réponses: 3
    Dernier message: 02/08/2005, 13h43

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