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 :

(debutant) lier 2 datagridview


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Novembre 2007
    Messages
    13
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 13
    Par défaut (debutant) lier 2 datagridview
    bonjour tout le monde
    j'ai deux datagridviews lies a deux tables dans ma base de données et ont en commun l'attribut "id_produit"

    mon probleme est de permettre a l'utilisateur de selectionner plusieurs produits dans le premier datagridview(checkbox column , nom_produit,id_produit)
    et de voir afficher sa selection dans le second datagridview

    voici mon code et une image de ma forms
    tout marche sauf biensur la fonction pr ce transfert(Private Sub bt_ajouter_Click)elle se trouve a la fin du 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
    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
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    Imports System.Data
    Imports system.data.sqlclient
     
    Public Class Form_enquete
     
    #Region "Declaration des variables"
        Dim ChaineConn As String = "data source=localhost;initial catalog=GestionDesPrix;user id=sa;password=sa"
        Dim Cn As New SqlConnection(ChaineConn)
        Dim ObjDa As SqlDataAdapter
        Dim ObjDa2 As SqlDataAdapter
        Dim ObjDs As New DataSet()
     
        Dim strsql As String
        Dim ObjDtable As DataTable
        Dim objDcolumn As DataColumn
        Dim ObjDrow As DataRow
        Dim rownumber As Integer
    #End Region
     
    #Region "Fonctions"
        Private Sub Remplir_DGV_enquete(ByVal DGView As DataGridView, ByVal DatS As DataTable)
            With DGView
                .Columns.Clear()
                .AllowUserToOrderColumns = True
                .AllowUserToDeleteRows = True
                .AllowUserToAddRows = True
                .AlternatingRowsDefaultCellStyle.BackColor = SystemColors.InactiveCaptionText
                .AutoGenerateColumns = False
                .DataSource = DatS
                .MultiSelect = False
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                .RowHeadersVisible = False
     
     
            End With
     
            Dim colTextBox As DataGridViewTextBoxColumn
            Dim colsupp As DataGridViewLinkColumn
            Dim colcombobox As DataGridViewComboBoxColumn
     
     
            colsupp = New DataGridViewLinkColumn()
            With colsupp
     
                .HeaderText = ""
                .Name = "SUPP"
                .Width = 24
                .Text = "X"
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                .UseColumnTextForLinkValue = True
                .TrackVisitedState = True
            End With
            DGView.Columns.Add(colsupp)
     
     
     
     
            colcombobox = New DataGridViewComboBoxColumn()
            With colcombobox
                .DataSource = ObjDs.Tables("PRODUIT")
                .ValueMember = "ID_PRODUIT"
                .DisplayMember = "NOM_PRODUIT"
                .DataPropertyName = "ID_PRODUIT"
                .HeaderText = "Produit"
                .Name = "ID_PRODUIT"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
            DGView.Columns.Add(colcombobox)
     
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "PRIX_MIN"
                .HeaderText = "Prix-Min"
                .Name = "PRIX_MIN"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
     
            End With
            DGView.Columns.Add(colTextBox)
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "PRIX_MAX"
                .HeaderText = "Prix-Max"
                .Name = "PRIX_MAX"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
            DGView.Columns.Add(colTextBox)
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "PRIX_FREQ"
                .HeaderText = "Prix-Frequent"
                .Name = "PRIX_FREQ"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
            DGView.Columns.Add(colTextBox)
     
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "date_vente"
                .HeaderText = "date"
                .Name = "date_vente"
                .Visible = False
            End With
            DGView.Columns.Add(colTextBox)
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "nom_marche"
                .HeaderText = "nom_marche"
                .Name = "nom_marche"
                .Visible = False
            End With
            DGView.Columns.Add(colTextBox)
     
     
        End Sub
     
        Private Sub Remplir_DGV_produit(ByVal DGView As DataGridView, ByVal DatS As DataTable)
            With DGView
                .Columns.Clear()
                .AllowUserToOrderColumns = True
                .AllowUserToDeleteRows = False
                .AllowUserToAddRows = False
                .AutoGenerateColumns = False
                .DataSource = DatS
                .MultiSelect = False
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                .RowHeadersVisible = False
     
     
            End With
     
            Dim colTextBox As DataGridViewTextBoxColumn
            Dim ocolCheck As DataGridViewCheckBoxColumn
     
            ocolCheck = New DataGridViewCheckBoxColumn()
            With ocolCheck
                .HeaderText = ""
                .Name = "Sup"
                .TrueValue = 1
                .FalseValue = 0
                .Width = 44
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
            DGView.Columns.Add(ocolCheck)
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "nom_produit"
                .HeaderText = "Produits"
                .Name = "nom_produit"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
     
            End With
            DGView.Columns.Add(colTextBox)
     
     
     
     
            colTextBox = New DataGridViewTextBoxColumn()
            With colTextBox
                .DataPropertyName = "id_produit"
                .HeaderText = "id_produit"
                .Name = "id_produit"
                .Visible = False
     
            End With
            DGView.Columns.Add(colTextBox)
     
     
     
     
     
     
        End Sub
    #End Region
     
        Private Sub Form_enquete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Panel1.Width = Me.Width - 36
            DGV_enquete.Visible = False
            Dgv_produit.Visible = False
            Bt_enregistrer.Visible = False
            bt_ajouter.Visible = False
     
            Try
     
                If Cn.State = ConnectionState.Closed Then Cn.Open()
     
                ObjDa = New SqlDataAdapter("SELECT * FROM marchedegros ORDER BY nom_marche", Cn)
                ObjDa.Fill(ObjDs, "marchedegros")
     
                Cb_marche.DisplayMember = "nom_marche"
                Cb_marche.ValueMember = "nom_marche"
                Cb_marche.DataSource = ObjDs.Tables("marchedegros")
     
     
                ObjDa = New SqlDataAdapter("SELECT id_produit,nom_produit FROM produit ORDER BY nom_produit ", Cn)
                ObjDa.Fill(ObjDs, "produit")
     
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Cn.Close()
     
     
     
     
        End Sub
     
        Private Sub DGV_enquete_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV_enquete.CellContentClick
            Dim dv As DataView
            If e.RowIndex > -1 Then
                If e.ColumnIndex = 0 Then
                    If DGV_enquete.CurrentRow IsNot Nothing Then
                        If MessageBox.Show("Voulez-vous vraiment supprimer cette ligne", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = vbYes Then
                            dv = ObjDs.Tables("estvendu").DefaultView
                            If dv.Count > 0 Then dv(e.RowIndex).Delete()
                        End If
                    End If
                End If
            End If
        End Sub
     
        Private Sub Bt_enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_enregistrer.Click
            Try
                If Cn.State = ConnectionState.Closed Then Cn.Open()
                Dim ObjCommandBuilder As New SqlCommandBuilder(ObjDa)
                ObjDa.Update(ObjDs, "estvendu")
     
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Cn.Close()
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
     
                If Cn.State = ConnectionState.Closed Then Cn.Open()
     
                ObjDa = New SqlDataAdapter("SELECT * FROM estvendu where date_vente='" & cb_date.Value & "' and nom_marche = '" & Cb_marche.SelectedValue & "'  ", Cn)
                ObjDa.Fill(ObjDs, "estvendu")
     
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            Cn.Close()
     
            Dgv_produit.Width = 200
            Dgv_produit.Height = (Me.Height) * 3.3 / 5
            Dgv_produit.Visible = True
     
            DGV_enquete.Width = Me.Width - 270
            DGV_enquete.Height = (Me.Height) * 3.3 / 5
            DGV_enquete.Visible = True
     
            Bt_enregistrer.Visible = True
            bt_ajouter.Visible = True
     
            Remplir_DGV_produit(Dgv_produit, ObjDs.Tables("produit"))
            Remplir_DGV_enquete(DGV_enquete, ObjDs.Tables("estvendu"))
     
        End Sub
     
        'Private Sub DGV_produit_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Dgv_produit.CellContentClick
     
        '    If e.RowIndex > -1 Then
        '        If e.ColumnIndex = 0 Then
        '            If Dgv_produit.CurrentRow IsNot Nothing Then
     
        '                Label3.Text = Dgv_produit.Rows(Dgv_produit.CurrentRow.Index).Cells("id_produit").Value
        '                MessageBox.Show("" & Dgv_produit.Rows(Dgv_produit.CurrentRow.Index).Cells("sup").ValueType.ToString & "  ")
     
        '            End If
        '        End If
        '    End If
        'End Sub
     
     
     
     
        Private Sub bt_ajouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_ajouter.Click
            Dim n, i As Integer
            n = Dgv_produit.RowCount
            For i = 0 To n - 1
                If Dgv_produit.Rows(i).Cells("sup").Value Then
     
                    'ajout ds lautre
                    'creation dune nouvelle row
     
                    'DGV_enquete.Rows.Add()
                    'DGV_enquete.Rows(DGV_enquete.CurrentRow.Index).Cells("id_produit").Value = Dgv_produit.Rows(i).Cells("id_produit").Value
                    'DGV_enquete.Rows(DGV_enquete.CurrentRow.Index).Cells("date_vente").Value = cb_date.Value
                    'DGV_enquete.Rows(DGV_enquete.CurrentRow.Index).Cells("nom_marche").Value = Cb_marche.SelectedValue
     
                    Dim ligne As DataRow
                    ligne = ObjDs.Tables("estvendu").NewRow
                    ligne("id_produit") = Dgv_produit.Rows(i).Cells("id_produit").Value
                    ligne("nom_marche") = "" & Cb_marche.SelectedValue & ""
                    ligne("date_vente") = "" & cb_date.Value & ""
                    DGV_enquete.Rows.Add(ligne)
     
     
     
                End If
            Next
     
     
     
        End Sub
    End Class

    merci d'avance pour votre aide

    j'ai pas pu joindre l'image

  2. #2
    Membre averti
    Inscrit en
    Novembre 2007
    Messages
    13
    Détails du profil
    Informations forums :
    Inscription : Novembre 2007
    Messages : 13
    Par défaut
    on n ajoute pas une datarow au datagridview mais on lajoute plutot a la datatable
    donc seule erreur est dans la derniere ligne de la fonction

    ObjDs.Tables("estvendu").Rows.Add(ligne)

    et voila j ai trouve tout seul la reponse comme un grand

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

Discussions similaires

  1. lier un datagridview avec un tableau
    Par kelvir dans le forum C#
    Réponses: 3
    Dernier message: 20/05/2010, 17h17
  2. Lier 2 datagridview
    Par droliprane dans le forum VB.NET
    Réponses: 8
    Dernier message: 27/08/2008, 13h27
  3. DataTable lier au DataGridView et filtre
    Par matrix_ceg dans le forum Windows Forms
    Réponses: 9
    Dernier message: 17/05/2008, 19h12
  4. [DataGridView] Comment lier un DataSet ?
    Par Ticoche dans le forum Windows Forms
    Réponses: 7
    Dernier message: 17/12/2007, 10h32
  5. Réponses: 3
    Dernier message: 22/11/2006, 10h35

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