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 :

Probleme de dataGridView, pour ajouter une ligne


Sujet :

C#

  1. #1
    Membre confirmé Avatar de Flow_75
    Femme Profil pro
    Ingénieure
    Inscrit en
    Mai 2005
    Messages
    1 096
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 41
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieure
    Secteur : Transports

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 096
    Points : 633
    Points
    633
    Par défaut Probleme de dataGridView, pour ajouter une ligne
    Bonjour,

    J'ai un probleme concernant un datagridview en winform.
    J'ai fais un code qui me permet de lister un nombre de chemin de fichier.
    J'ai binder une Liste de StringValue (via : https://stackoverflow.com/questions/...idview-control

    J'ai autorisé l'ajout de nouvelle ligne dans la binding source ainsi que pour le datagridview.
    Je peux ajouter une première valeur sans soucis. Mais la ligne blanche me permettant d'ajouter des données n'apparait pas après.

    Pourriez vous m'aider ?
    Merci.
    F.

    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
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
     
    namespace CockPit2018
    {
        public partial class SelectionMFichier: Form
        {
            public SelectionMFichier()
            {
                InitializeComponent();
            }
     
            public List<StringValue> Listbindingfiles = new List<StringValue>();
     
            private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                if(e.RowIndex == -1 || e.ColumnIndex == -1)
                    return;
     
                OpenFileDialog ofd = new OpenFileDialog();
     
                if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = ofd.FileName;
                }
     
                dataGridView1.EndEdit();
            }
     
            public List<string> GetFilesList()
            {
                return Listbindingfiles.Select(sv => sv.Value).ToList();
            }
     
            public void SetFileList(List<string> filelist)
            {
                Listbindingfiles.Clear();
     
                foreach(string file in filelist)
                    Listbindingfiles.Add(new StringValue(file));
            }
        }
     
        public class StringValue
        {
            public StringValue() { }
     
            public StringValue(string s)
            {
                _value = s;
            }
            public string Value { get { return _value; } set { _value = value; } }
            string _value;
        }
    }
    Le coté designer :
    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
     
            private void InitializeComponent()
            {
                this.components = new System.ComponentModel.Container();
                System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                this.valueDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
                this.stringValueBindingSource = new System.Windows.Forms.BindingSource(this.components);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                ((System.ComponentModel.ISupportInitialize)(this.stringValueBindingSource)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.AllowUserToAddRows = true;
                this.dataGridView1.AllowUserToResizeColumns = false;
                this.dataGridView1.AllowUserToResizeRows = false;
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
                dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
                dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
                dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
                dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
                dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
                dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
                this.dataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.valueDataGridViewTextBoxColumn});
                this.dataGridView1.DataSource = this.stringValueBindingSource;
                this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.dataGridView1.Location = new System.Drawing.Point(0, 0);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowHeadersVisible = false;
                this.dataGridView1.Size = new System.Drawing.Size(532, 185);
                this.dataGridView1.TabIndex = 0;
                this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
                // 
                // valueDataGridViewTextBoxColumn
                // 
                this.valueDataGridViewTextBoxColumn.DataPropertyName = "Value";
                this.valueDataGridViewTextBoxColumn.HeaderText = "Chemin des fichiers";
                this.valueDataGridViewTextBoxColumn.Name = "valueDataGridViewTextBoxColumn";
                // 
                // stringValueBindingSource
                // 
                this.stringValueBindingSource.AllowNew = true;
                this.stringValueBindingSource.DataSource = typeof(CockPit2018.StringValue);
                // 
                // SelectionMFichier
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(532, 185);
                this.Controls.Add(this.dataGridView1);
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "SelectionMFichier";
                this.ShowIcon = false;
                this.ShowInTaskbar = false;
                this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
                this.Text = "Selection Fichiers";
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                ((System.ComponentModel.ISupportInitialize)(this.stringValueBindingSource)).EndInit();
                this.ResumeLayout(false);
     
            }
     
            #endregion
     
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.DataGridViewTextBoxColumn valueDataGridViewTextBoxColumn;
            private System.Windows.Forms.BindingSource stringValueBindingSource;
        }

  2. #2
    Membre actif
    Homme Profil pro
    Autodidacte
    Inscrit en
    Mars 2016
    Messages
    154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Brazzaville

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

    Informations forums :
    Inscription : Mars 2016
    Messages : 154
    Points : 268
    Points
    268
    Par défaut pensez à utiliser les balises de code
    Bonjour mon cher,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
            {
                if(e.RowIndex == -1 || e.ColumnIndex == -1)
                    return;
                 OpenFileDialog ofd = new OpenFileDialog();
     
                if(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    dataGridView1.Rows[e.RowIndex].Cells[0].Value = ofd.FileName;
                }
                 dataGridView1.EndEdit();
            }
    y a t il du code derière cette methode ? dataGridView1.EndEdit();.
    Et pourquoi n'utilises tu pas la propriété AllowUserToAddRows pour forcer le datagriview à ajouter une nouvelle ligne ?

  3. #3
    Membre du Club
    Homme Profil pro
    Etudiant
    Inscrit en
    Janvier 2016
    Messages
    54
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Etudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 54
    Points : 64
    Points
    64
    Par défaut
    Si allowUserToAddRow était à False, il ne pourrait même pas en ajouter une --> Du coup euh... endEdit() devrait pas être le problème mais tu peux toujours essayer de l'enlever.
    Il y a quelque chose dans ton code qui fait que ça marche chez nous et pas chez toi !

    Ceci dit, tu as plein d'autres façons pour ajouter une ligne, cela dépend de ce que tu veux faire avec.

Discussions similaires

  1. Création de boutons pour ajouter une ligne
    Par jenor dans le forum Général VBA
    Réponses: 2
    Dernier message: 23/06/2015, 10h57
  2. [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
  3. Macro pour ajouter une ligne dans une feuille automatiquement
    Par nianko dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 09/11/2010, 09h52
  4. Comment faire pour ajouter une ligne seulement !
    Par Fredri dans le forum Access
    Réponses: 1
    Dernier message: 26/12/2005, 11h44

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