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

VB.NET Discussion :

Ajouter une ligne TOTAL (non modifiable) en bas d'un DATAGRIDVIEW [Débutant]


Sujet :

VB.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2017
    Messages
    140
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2017
    Messages : 140
    Par défaut Ajouter une ligne TOTAL (non modifiable) en bas d'un DATAGRIDVIEW
    Bonjour,
    J'ai créé une petite appli où j'utilise un DATAGRIDVIEW dont la source est un DATASET pour visualiser les éléments d'une BDD SQL server 2008 R2.
    Jusque-là pas de problème. Je peux modifier directement les données dans les cellules du DATAGRIDVIEW pour Updater ma BDD
    Maintenant je souhaiterais ajouter une ligne en bas de mon DATAGRIDVIEW pour indiquer le TOTAL de certaines colonnes (Débit et Crédit), sans que bien sûr cette ligne soit modifiable et surtout sans que ma BDD soit Updatée par cette nouvelle ligne.
    Nom : Capture d'écran 2017-08-20 20.17.04.png
Affichages : 861
Taille : 54,0 Ko

    Donc ma question : Comment ajouter une ligne TOTAL en bas du DATAGRIDVEW sans que son contenu soit modifiable dans les cellules et surtout sans impacter ma BDD ?

    Merci pour votre aide précieuse

  2. #2
    Membre très actif Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    941
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 941
    Par défaut
    A vrai dire, je ne sais pas s'il est possible de faire ce que tu cherches à réaliser avec un DataGridView.
    Par contre, tu pourrais utiliser deux TextBox non modifiables pour afficher tes totaux en débit/crédit.
    .

  3. #3
    Membre confirmé
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2017
    Messages
    140
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2017
    Messages : 140
    Par défaut
    J'y ai pensé, le problème c'est que les colonnes du DataGridView sont redimensionnables en fonction de leur contenu et du coup les TextBox ne sont plus au bon endroit. J'ai aussi envisagé un deuxième DataGridView pour les totaux juste en dessous mais j'ai le même problème.

  4. #4
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour
    Le seul moyen est de "peindre" ce qu'on veut obtenir dans la ou les colonnes souhaites ,après avoir calcule le solde ,avec l'event CellPainting

    code exemple .vb tiré de la MSDN Help Library.....
    -DataSet +TableAdapter +BindingSource1 droppé sur le designer VS
    -un DGV
    -La table Personnel avec les champs: NOM,CREDIT ,DEBIT
    L'event est géré 2 fois :
    -pour peindre le libelle "Solde :" dans la colonne "NOM" du dgv
    -pour peindre le solde calculé dans la colonne "CREDIT" du dgv

    La peinture se fait au clic dans l'une ou l'autre cellule du nouveau Row (astérisque)
    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
     
    Public Class Form2
     
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            PersonnelTableAdapter1.Fill(PersonnelDataSet1.Personnel)
     
     
        End Sub
     
        Private Sub DGV_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DGV.CellPainting
            If Me.DGV.Columns(0).Index = e.ColumnIndex AndAlso e.RowIndex >= DGV.Rows.Count - 1 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.DGV.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 (IsDBNull(e.Value)) Then
                        Dim libelle As String = "Solde :"
                        e.Graphics.DrawString(libelle, 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
            If Me.DGV.Columns(1).Index = e.ColumnIndex AndAlso e.RowIndex >= DGV.Rows.Count - 1 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.DGV.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 (IsDBNull(e.Value)) Then
                        Dim sum As Single = CalcSolde()
                        e.Graphics.DrawString(sum, 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
        Private Function CalcSolde()
            Dim solde As Single = 0.0
            For i As Integer = 0 To DGV.Rows.Count - 2
                solde += DGV.Rows(i).Cells(1).Value - DGV.Rows(i).Cells(2).Value
            Next
            Return solde
        End Function
     
     
    End Class
    Bon code ...

  5. #5
    Membre confirmé
    Homme Profil pro
    Enseignant
    Inscrit en
    Mars 2017
    Messages
    140
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Enseignant
    Secteur : Enseignement

    Informations forums :
    Inscription : Mars 2017
    Messages : 140
    Par défaut
    Merci MABROUKI
    Je vais essayer de mettre ça en oeuvre

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 19/05/2016, 09h23
  2. Réponses: 2
    Dernier message: 04/07/2015, 15h24
  3. Réponses: 0
    Dernier message: 17/10/2013, 09h53
  4. Ajouter une ligne "Total"
    Par kedmard dans le forum Requêtes et SQL.
    Réponses: 1
    Dernier message: 19/01/2012, 18h17
  5. Réponses: 4
    Dernier message: 25/02/2011, 09h20

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