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 �prouv� Avatar de Flow_75
    Femme Profil pro
    Ing�nieure
    Inscrit en
    Mai 2005
    Messages
    1 104
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    �ge : 42
    Localisation : France, Paris (�le de France)

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

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 104
    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 tr�s 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
    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 averti
    Homme Profil pro
    Etudiant
    Inscrit en
    Janvier 2016
    Messages
    54
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    �ge : 29
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activit� : Etudiant

    Informations forums :
    Inscription : Janvier 2016
    Messages : 54
    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