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

C# Discussion :

changer le type des cellules d'une ligne dans un datagridview


Sujet :

C#

  1. #1
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut changer le type des cellules d'une ligne dans un datagridview
    Bonjour, comment changer le type des cellules d'une ligne dans un datagridview, j'ai mis :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
                    this.dataGridView1[i, 0] = new DataGridViewTextBoxCell();
    mais ile me donne l'erreur suivante :

    La valeur mise en forme de la cellule est d'un type incorrecte

  2. #2
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Sans en être totalement sur, je ne pense pas qu'il soit possible de définir des types de cellules différentes dans une même colonne de DataGridView.

    Pour imaginer une autre solution :
    Quel est l'objectif ?
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  3. #3
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    Le but : dans un meme datagridview il y a des lignes qui doivent avoir des case a cocher et d'autre Non.

  4. #4
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Solution standard : 2 colonnes de type différent, mais ce ne sera peut-être pas trop joli.

    Plus compliqué, gérer l'event CellPainting pour dessiner les 2 types (carré de CheckBox ou texte) et traiter les event MouseDown pour modifier l'"état" et le dessin de la CheckBox.
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  5. #5
    Membre habitué
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Points : 175
    Points
    175
    Par défaut
    veillez m'initier parce sur google je ne trouve pas d'exemple pour le changement de cellule de checkbox en cellule normale sans text sans rien du tout pour que je puisse changer son arrière plan a chaque fois que j'en ai besoin

  6. #6
    Expert éminent Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Points : 7 903
    Points
    7 903
    Par défaut
    Si le problème est de mettre soit une check box soit du texte non editable, on utilisera une colonne de type CheckBox.

    Dans le cas CheckBox :
    1) dans l'event CellBeginEdit, mettre la Propriété ReadOnly de la cell à false,
    2) Dans l'event Cellpainting, faire simplement e.Handled=false

    Dans le cas CheckBox :
    3) dans l'event CellBeginEdit, mettre la Propriété ReadOnly de la cell à true,
    4) Dans l'event Cellpainting, dessiner le Text et faire simplement e.Handled=true,

    A essayer : le CellBeginEdit ne convient peut-être pas pour les colonnes CheckBox .
    " Le croquemitaine ! Aaaaaah ! Où ça ? " ©Homer Simpson

  7. #7
    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 441
    Points
    4 441
    Par défaut CheckBoxColumn déguisée en Incognito
    bonjour Msysteme

    Cela est possible comme l'as dit Graffito.Le cote fort intriguant dans ceci c'est comment tu vas le gerer .
    Car tu as avoir des CheckBox peints en "CheckBox" et des CheckBox peints en "nothing", neant,itou....
    Pour pouvoir decider comment peindre il va falloir qu tu reflichess sur le critere à utiliser .Un bon critere de type valeur numerique "peu variable ou qui varient dans des limites controlees" et provenant d'une cellule sur la meme ligne que le CheckBox.
    voici un exemple tire de MSDN Doc pour l'event DataGridView1_CellPainting et modifie pour toi:
    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
     
    Public Class Form2
     
    	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    		AddColorColumn()
    		'Mettre False comme valeur par defaut pour tous les
    		'Checkbox
    		'Ajout 10 clients
    		For i As Integer = 1 To 10
    			If i = 3 Or i = 5 Then
    				Me.DataGridView1.Rows.Add("ClientWithoutCheck", False, 1000.0 * i)
    			End If
    			Me.DataGridView1.Rows.Add("ClientWithCheck", False, 2000.0 * i)
    		Next
     
    	End Sub
     
    	Private Sub AddColorColumn()
    		'Ajout 1ere colonne  DataGridViewTextBoxColumn
    		Dim textBoxColumn1 As New DataGridViewTextBoxColumn()
    		textBoxColumn1.HeaderText = "Client"
    		textBoxColumn1.ValueType = GetType(String)
    		DataGridView1.Columns.Add(textBoxColumn1)
     
    		'Ajout 2eme colonne  DataGridViewCheckBoxColumn
    		Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
    		checkBoxColumn.HeaderText = "myCheckBox"
    		checkBoxColumn.ValueType = GetType(Boolean)
    		checkBoxColumn.Name = "myCheckBox"
    		DataGridView1.Columns.Add(checkBoxColumn)
     
    		'Ajout 3eme colonne  DataGridViewTextBoxColumn
    		Dim textBoxColumn2 As New DataGridViewTextBoxColumn()
    		textBoxColumn2.HeaderText = "Price"
    		textBoxColumn2.ValueType = GetType(Double)
    		DataGridView1.Columns.Add(textBoxColumn2)
    	End Sub
     
    	Private Sub dataGridView1_CellPainting(ByVal sender As Object, _
    	ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) _
    	Handles DataGridView1.CellPainting
     
    		If Me.DataGridView1.Columns("myCheckBox").Index = _
    		 e.ColumnIndex AndAlso e.RowIndex >= 0 AndAlso _
    		 (Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value = 3000 Or Me.DataGridView1.Rows(e.RowIndex).Cells(2).Value = 5000) Then
     
    			Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
    			 e.CellBounds.Width - 4, e.CellBounds.Height - 4)
    			Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor)
    			Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor)
    			Dim gridLinePen As New Pen(gridBrush)
     
    			Try
     
    				' Erase the cell.
    				e.Graphics.FillRectangle(backColorBrush, e.CellBounds)
     
    				' Draw the grid lines (only the right and bottom lines;
    				' DataGridView takes care of the others).
    				e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
    				 e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
    				 e.CellBounds.Bottom - 1)
    				e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
    				 e.CellBounds.Top, e.CellBounds.Right - 1, _
    				 e.CellBounds.Bottom)
     
    				' Draw the inset highlight box.
    				e.Graphics.DrawRectangle(Pens.Blue, newRect)
     
    				' Draw the text content of the cell, ignoring alignment.
    				'If (e.Value IsNot Nothing) Then
    				'	e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
    				'	Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
    				'	StringFormat.GenericDefault)
    				'End If
    				If (e.Value IsNot Nothing) Then
    					e.Graphics.DrawString("", e.CellStyle.Font, _
    					Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
    					StringFormat.GenericDefault)
    				End If
    				e.Handled = True
     
    			Finally
    				gridLinePen.Dispose()
    				gridBrush.Dispose()
    				backColorBrush.Dispose()
    			End Try
     
    		End If
     
    	End Sub
    	'Un bouton pour tester qu'on n'accede au CheckBox quand on clique dessus
    	'car la peinture ne suffit à elle seule
    	Private Sub DataGridView1_CellValueChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    		Dim str As String = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
    		MessageBox.Show(str)
    	End Sub
    End Class
    Un bouton a ete rajoute pour tester qu'on n'accede pas au CheckBox "deguisé" quand on clique dessus.....mais dis-moi as-tu MSDN Doc sur ta machine ou pas?.... .
    J'avoue que repeindre le CheckBox n'est pas chose difficile ,c'est gerer la suite de ta logique qui m' a paru plus difficile..........................
    bon code..............

  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 441
    Points
    4 441
    Par défaut
    Rebonjour Msysteme
    comme tu fais du C# voici la version C# du meme code.......excuse-moi....
    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace DataGridViewCheckBoxCsharp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Button1_Click(object sender, EventArgs e)
            {
                //Ajout 3 colonnes
                    AddColorColumn();
                //Mettre False comme valeur par defaut pour tous les
                //Checkbox
                //Ajout 10 clients
    		        for( int i =0;i<=10;i++ )
                    {
    			        if( i == 3 || i == 5)
                        {
                             this.DataGridView1.Rows.Add("ClientWithoutCheck", false, 1000.0 * i);
                        }
    			        else
                        {
                            this.DataGridView1.Rows.Add("ClientWithCheck", false, 2000.0 * i);
                        }
     
    		         }
            }
            private void AddColorColumn()
            {
            //Ajout 1ere colonne  DataGridViewTextBoxColumn
            DataGridViewTextBoxColumn textBoxColumn1 = new DataGridViewTextBoxColumn();
    		textBoxColumn1.HeaderText = "Client";
    		textBoxColumn1.ValueType =typeof(String);
            this.DataGridView1.Columns.Add(textBoxColumn1);
     
            //Ajout 2eme colonne  DataGridViewCheckBoxColumn
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
    		checkBoxColumn.HeaderText = "myCheckBox";
            checkBoxColumn.ValueType = typeof(Boolean);
    		checkBoxColumn.Name = "myCheckBox";
            this.DataGridView1.Columns.Add(checkBoxColumn);
     
            //'Ajout 3eme colonne  DataGridViewTextBoxColumn
            DataGridViewTextBoxColumn textBoxColumn2 =new DataGridViewTextBoxColumn();
    		textBoxColumn2.HeaderText = "Price";
            textBoxColumn2.ValueType = typeof(Double);
            this.DataGridView1.Columns.Add(textBoxColumn2);
     
            }
     
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (this.DataGridView1.Columns["myCheckBox"].Index ==
           e.ColumnIndex && e.RowIndex >= 0 )
     
                {
                    if ((double)this.DataGridView1.Rows[e.RowIndex].Cells[2].Value == 3000.0 | (double)this.DataGridView1.Rows[e.RowIndex].Cells[2].Value == 5000.0)
                    {
                        Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
                        e.CellBounds.Y + 1, e.CellBounds.Width - 4,
                        e.CellBounds.Height - 4);
     
                        using (
                            Brush gridBrush = new SolidBrush(this.DataGridView1.GridColor),
                            backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                        {
                            using (Pen gridLinePen = new Pen(gridBrush))
                            {
                                // Erase the cell.
                                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
     
                                // Draw the grid lines (only the right and bottom lines;
                                // DataGridView takes care of the others).
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
                                    e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
                                    e.CellBounds.Bottom - 1);
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
                                    e.CellBounds.Top, e.CellBounds.Right - 1,
                                    e.CellBounds.Bottom);
     
                                // Draw the inset highlight box.
                                e.Graphics.DrawRectangle(Pens.Blue, newRect);
     
                                //commented. Draw the text content of the cell, ignoring alignment.
                                //if (e.Value != null)
                                //{
                                //    e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                //        Brushes.Crimson, e.CellBounds.X + 2,
                                //        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                //}
                                //Le texte doit etre mis à blanc sinon il peint
                                //false
                                if (e.Value != null)
                                {
                                    e.Graphics.DrawString("", e.CellStyle.Font,
                                        Brushes.Crimson, e.CellBounds.X + 2,
                                        e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                }
                                e.Handled = true;
                            }
                        }
                    }  
    	         }        
     
            }
            //on teste le ckeck - uncheck du  CheckBox quand on clique dessus
            //car la peinture ne suffit pas à elle seule
            private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                String str   = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                MessageBox.Show(str);
            }
     
        }
    }
    bon code..........................

Discussions similaires

  1. [XL-2007] Cibler des cellules sur chaque ligne dans une sélection
    Par Ghuron dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 30/09/2014, 09h59
  2. [Java] Données des valeurs des cellules d'une ligne [Tableau]
    Par SoGeek dans le forum Interfaces Graphiques en Java
    Réponses: 3
    Dernier message: 17/04/2010, 15h40
  3. Réponses: 2
    Dernier message: 09/09/2009, 14h08
  4. changer les dimensions des cellules d'une table
    Par master_och dans le forum Composants
    Réponses: 3
    Dernier message: 26/09/2008, 19h12
  5. [C#][VS 2005]Changer la couleur d'une ligne dans un DataGridView
    Par Mast3rMind dans le forum Windows Forms
    Réponses: 5
    Dernier message: 13/07/2006, 23h20

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