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 :

Progressbar dans DataGridView


Sujet :

Windows Forms

  1. #1
    Membre du Club
    Homme Profil pro
    Responsable de rayon
    Inscrit en
    Juin 2005
    Messages
    86
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de rayon
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Juin 2005
    Messages : 86
    Points : 60
    Points
    60
    Par défaut Progressbar dans DataGridView
    Bonsoir
    Je cherche à mettre une barre de progression dans une cellule d'un DataGridView.
    Voici mon code :

    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
    private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.ColumnIndex != 2)
                    return;
                if (e.RowIndex < 0)
                    return;
                ProgressBar pb = new ProgressBar();
                const int margin = 4;
     
                pb.Width = e.CellBounds.Right - e.CellBounds.Left - (margin * 2);
                pb.Value = (int)e.Value;
                Bitmap bmp = new Bitmap(pb.Width, pb.Height);
                pb.Update();
                pb.DrawToBitmap(bmp, pb.ClientRectangle);
                e.Graphics.DrawImage(bmp, new Point(e.CellBounds.X +margin, ((e.CellBounds.Bottom - e.CellBounds.Top - pb.Height) / 2) + e.CellBounds.Top));
                e.Handled = true;
            }
    La barre de progression est bien là, sa valeur est bien à 50 mais visuellement la barre est à 0.
    Pourquoi et comment corriger ça ?

  2. #2
    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
    bonjour Kaneda Shotaro.
    Bizarre,bizarre ton code fonctionne chez moi.As-tu popule le dgv?
    voici ton code repris qui fonctionne chez 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
     
    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 DataGridViewProgressBarCSharp
    {
        public partial class Form1 : Form
        {
            private DataClass dt;
            private List<DataClass >  ListData=new List<DataClass> ();
                public Form1()
            {
                InitializeComponent();
     
                populateDGV();
            }
            private void populateDGV()
            {
     
                for (int i = 0; i < 9; i++)
    			{
                    this.dt = new DataClass("Nom" + (i + 1).ToString(), (i + 1) * 10);
                    this.ListData.Add(dt);
                }
     
                this.dataGridView1.DataSource=this.ListData ;
            }
     
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.ColumnIndex != 1 )
                      return ;
                    if (e.RowIndex < 0)
                        return;
                    ProgressBar pb = new ProgressBar();
                    const int margin = 4;
     
                    pb.Width = e.CellBounds.Right - e.CellBounds.Left - (margin * 2);
                    pb.Value = (int)e.Value;
     
                    Bitmap bmp = new Bitmap(pb.Width, pb.Height);
                    pb.Update();
                    pb.DrawToBitmap(bmp, pb.ClientRectangle);
                    e.Graphics.DrawImage(bmp, new Point(e.CellBounds.X + margin, ((e.CellBounds.Bottom - e.CellBounds.Top - pb.Height) / 2) + e.CellBounds.Top));
                    e.Handled = true;
            }
     
     
        }
        public  class DataClass 
        {
            public DataClass(string n, int num)
            {
               this.Name=n;
               this.Account = num;
            }
            public string Name { get; set; }
            public int Account { get; set; }
        }
    }
    bon code...........

  3. #3
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 749
    Points
    39 749
    Par défaut
    Je ne sais pas exactement ce qui pose problème, mais ce serait sans doute plus simple (et plus réutilisable) de créer un type de colonne spécifique, en s'inspirant de ce qui est fait ici

Discussions similaires

  1. [MFC] afficher une ProgressBar dans une barre d'etat
    Par guillaume21 dans le forum MFC
    Réponses: 5
    Dernier message: 30/03/2007, 11h16
  2. [vb 2005]Taille du texte dans datagridview
    Par estelledany dans le forum Windows Forms
    Réponses: 2
    Dernier message: 01/06/2006, 14h24
  3. Label et progressbar dans formulaire
    Par jean-pierre96 dans le forum IHM
    Réponses: 2
    Dernier message: 03/05/2006, 12h58
  4. [VB.Net] Une progressbar dans une statusbar
    Par tomnie dans le forum Windows Forms
    Réponses: 6
    Dernier message: 14/12/2005, 10h13
  5. [SWT] ProgressBar dans une Table
    Par spi dans le forum SWT/JFace
    Réponses: 2
    Dernier message: 12/01/2005, 13h58

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