Récupération des données ligne sélectionnée dans GridView

Ilovedev Messages postés 19 Statut Membre -  
Ilovedev Messages postés 19 Statut Membre -
Bonjour,

Tous mes voeux pour commencer !! :o)

J'ai un souci (tout bête j'epère!) que je vais tenter de vous exposer...
J'ai deux Gridview dans une même page. Le but est de récupérer les données de la ligne selectionnée dans la Grid1 dans la deuxième Grid2.
J'ai deux procédures stockées que j'appelle dans chacune d'elles. La première sélectionne les numéros de demande quand numDemande est 2 (par exemple), la deuxième récupère les données de la ligne selectionnée par rapport à son ID, entré en paramètre.
Le souci c'est que dès lors que j'affecte ma procèdure stockée à l'une, l'autre se réinitialise avec la même procédure stockée, ce que je ne veux pas faire evidemment.
Ai-je été claire ? Avez-vous une solution ?

Merci d'avance !

6 réponses

  1. Kalissi Messages postés 221 Statut Membre 20
     
    Bonjour,

    Quel Langage de programmation ?
    Quel type de BD ?
    Procédure stockée dans quel langage ?
    Utilise-tu un objet BindingSource pour tes DtatGridView.
    Utilise-tu une architecture organique pour accéder à ta BD.

    Ces éléments sont nécessaire pour savoir comment t'aider.

    Je si comprends, lorsque tu sélectionne un ligne dans Grid1, Grid2 se met à jour automatiquement !

    Pour récupérer les données de ta Grid1, ça devrait ressembler à quelque chose come suit :


    Private Sub DGV_Solutions_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGV_Solutions.SelectionChanged

    Dim Texte As New System.Text.StringBuilder
    Dim Indice As Int32 = Me.DGV_Solutions.CurrentRow.Index
    Dim ColIndice As Int32 = Me.DGV_Solutions.RowCount - 1
    For Boucle As Int32 = 0 To ColIndice
    Texte.AppendLine(DirectCast(Me.DGV_Solutions.Item(Boucle, Indice).Value, String))
    Next

    MessageBox.Show(Texte.ToString)

    End Sub

    K
    0
  2. Ilovedev Messages postés 19 Statut Membre
     
    Bonjour Kalissi et merci...

    Effectivement, j'ai donné très peu d'informations !

    Je programme en C# (technologie ASP.NET), et ma BD est sous SQL.

    Et comme tu l'as compris, lorsque je selectionne une ligne dans ma Grid 1, ma Grid 2 récupère les données composant cette ligne. J'aimerai que ma Grid 2 puisse être dans une autre page "Recap" et pouvoir supprimer ou modifier ces données dans ma BD par la suite.

    J'ai écrit mes procédures stockées directement dans SQL ou je déclare comme paramêtre l'ID de la ligne sélectionnée...

    J'espère être assez claire avec toi je débute et j'avoue que beaucoup de notions me sont encore aujourd'hui très abstraites.

    Si tu as une idée ou même un exemple de code, je t'en remercie vivement par avance :o)
    0
  3. Kalissi Messages postés 221 Statut Membre 20
     
    Salut,

    Malheureusement, je ne développe pas en CSharp, toutefois je peux tenter de t'expliquer le principe et te fournir des exemple de code en VB qui tu devrais pouvoir traduire en CSharp.

    Décomposer en modèle organique ta solution.

    Premier projet (Modele_Commun)


    ''' <summary>
    ''' Déclaration des constantes communes à l'ensemble de la solution PC_config
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Modele_Communaute

    #Region "--- Constantes ---"

    #Region "--- Constantes publiques à la solutions ---"

    ' Taper 3 apostrophes de façon systématique sur la ligne suivante :
    ' au même alinéa que le P de Public et le modèle de le doc xml apparaîtra

    Public Const iCol_id_Ind1 As Int32 = 0
    Public Const iCol_pseudo_Ind2 As Int32 = 1
    Public Const iCol_pass_Ind3 As Int32 = 2
    Public Const iCol_email_Ind4 As Int32 = 3
    Public Const iCol_date_inscr_Ind5 As Int32 = 4

    Public Const BDNomTable As String = "historique.membres"

    Public Const BD100Id As String = "id" ' Type String
    Public Const BD110Pseudo As String = "pseudo" ' Type String
    Public Const BD120Pass As String = "pass" ' Type String
    Public Const BD130Email As String = "email" ' Type String
    Public Const BD140DtInscr As String = "date_inscription" ' Type Date
    ' Dernier champ sinon augmenter les indices de collonnes
    ' précédément définis

    #End Region

    #End Region

    End Class

    Deuxième projet - 3 Classes
    Première classe (Modele_Entite) - L'entité



    ''' <summary>
    ''' Facette de la BD selon la table [ membre ]
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ModeleEntite

    #Region "--- Région Attributs ---"

    ' TODO - Remplacer les noms d'attributs pas des noms significatifs
    Private zBD100Id As String = String.Empty
    Private zBD110Pseudo As String = String.Empty
    Private zBD120Pass As String = String.Empty
    Private zBD130Email As String = String.Empty
    Private zBD140DtInscr As Date

    Private zEstNouveau As Boolean = False
    Private zEstModifier As Boolean = False
    Private zEstSupprimer As Boolean = False

    #End Region

    #Region "--- Région Propriétés ---"

    ' TODO - Remplacer les noms de propriétéss pas des noms significatifs

    Public Property id() As String
    Get
    Return Me.zBD100Id
    End Get
    Set(ByVal value As String)
    Me.zBD100Id = value
    End Set
    End Property

    Public Property pseudo() As String
    Get
    Return Me.zBD110Pseudo
    End Get
    Set(ByVal value As String)
    Me.zBD110Pseudo = value
    End Set
    End Property

    Public Property pass() As String
    Get
    Return Me.zBD120Pass
    End Get
    Set(ByVal value As String)
    Me.zBD120Pass = value
    End Set
    End Property

    Public Property email() As String
    Get
    Return Me.zBD130Email
    End Get
    Set(ByVal value As String)
    Me.zBD130Email = value
    End Set
    End Property

    Public Property date_inscription() As Date
    Get
    Return Me.zBD140DtInscr
    End Get
    Set(ByVal value As Date)
    Me.zBD140DtInscr = value
    End Set
    End Property

    Public Property EstModifier() As Boolean
    Get
    Return Me.zEstModifier
    End Get
    Set(ByVal value As Boolean)
    Me.zEstModifier = value
    End Set
    End Property

    Public Property EstNouveau() As Boolean
    Get
    Return Me.zEstNouveau
    End Get
    Set(ByVal value As Boolean)
    Me.zEstNouveau = value
    End Set
    End Property

    Public Property EstSupprimer() As Boolean
    Get
    Return Me.zEstSupprimer
    End Get
    Set(ByVal value As Boolean)
    Me.zEstSupprimer = value
    End Set
    End Property

    #End Region

    #Region "--- Région Méthodes ---"

    #Region "--- Région Constructeur ---"

    ''' <summary>
    ''' Constructeur par défaut
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    #End Region

    #End Region

    End Class

    Deuxième projet
    Deuxième classe (Modele_Criteres) - Les critères de requêtes


    Public Class ModeleCriteres

    #Region "--- Région Attributs ---"

    ' TODO - Remplacer les nom d'attributs par des noms significatifs

    Private zBD100Id As String = String.Empty
    Private zBD110Pseudo As String = String.Empty
    Private zBD120Pass As String = String.Empty
    Private zBD130Email As String = String.Empty
    Private zBD140DtInscr As Date

    Private zCheminBD As String = String.Empty

    #End Region

    #Region "--- Région Propriétés ---"

    ' TODO - Remplacer les nom de propriétés par des noms significatifs

    Public Property id() As String
    Get
    Return Me.zBD100Id
    End Get
    Set(ByVal value As String)
    Me.zBD100Id = value
    End Set
    End Property

    Public Property pass() As String
    Get
    Return Me.zBD110Pseudo
    End Get
    Set(ByVal value As String)
    Me.zBD110Pseudo = value
    End Set
    End Property

    Public Property pseudo() As String
    Get
    Return Me.zBD120Pass
    End Get
    Set(ByVal value As String)
    Me.zBD120Pass = value
    End Set
    End Property

    Public Property email() As String
    Get
    Return Me.zBD130Email
    End Get
    Set(ByVal value As String)
    Me.zBD130Email = value
    End Set
    End Property

    Public Property Cinquieme() As Date
    Get
    Return Me.zBD140DtInscr
    End Get
    Set(ByVal value As Date)
    Me.zBD140DtInscr = value
    End Set
    End Property

    Public Property CheminBD() As String
    Get
    Return Me.zCheminBD
    End Get
    Set(ByVal value As String)
    Me.zCheminBD = value
    End Set
    End Property

    #End Region

    #Region "--- Région Méthodes ---"

    #Region "--- Région Constructeur ---"

    Public Sub New(ByVal pChemin As String)
    CheminBD = pChemin
    End Sub

    #End Region

    #End Region

    End Class

    Deuxième projet
    Troisième Classe (Modele_ListeTable) - Les listes fortements typés



    ''' <summary>
    ''' Classe de liste de la collection d'objets PC_config_Table
    ''' </summary>
    ''' <remarks></remarks>
    Public Class ModeleListeTable

    Inherits System.Collections.Generic.List(Of Modele_Entite.ModeleEntite)

    End Class

    Troisième projet - 3 Classes
    Première classe - Les requêtes



    #Region "--- Importations des classes externes ---"

    Imports TYP_ENT = Modele_Entite.ModeleEntite
    Imports TYP_CRI = Modele_Entite.ModeleCriteres
    Imports TYP_COM = Modele_Commun.Modele_Communaute

    #End Region

    Public Class Modele_ReqSQL

    #Region "--- Région Constantes SQL ---"

    #Region "--- Région Requête SQL Insertion ---"

    ' TODO - Note la syntaxe peut changer selon le type de la BD

    'INSERT INTO 'membre' ('id', 'pseudo', 'pass', 'email', 'date_inscription') VALUES
    '(NULL, 'guillaume', '85ca51916e72aafad09b20409bbc43d538f4d791', 'guillaume.robier@hotmail.fr', 2012);

    Public ReadOnly Property Insertion(ByVal pEnt As TYP_ENT) As String
    Get
    Dim ChaineSQL As New System.Text.StringBuilder

    ChaineSQL.Append("INSERT INTO ")
    ChaineSQL.Append(TYP_COM.BDNomTable)
    ChaineSQL.Append(" (")
    ChaineSQL.Append(TYP_COM.BD100Id)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD110Pseudo)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD120Pass)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD130Email)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD140DtInscr)
    ChaineSQL.Append(") ")
    ChaineSQL.Append(" VALUES (")
    ChaineSQL.Append("'" & pEnt.id & "', ")
    ChaineSQL.Append("'" & pEnt.pseudo & "', ")
    ChaineSQL.Append("'" & pEnt.pass & "', ")
    ChaineSQL.Append("'" & pEnt.email & "', ")
    ChaineSQL.Append("'" & pEnt.date_inscription & "')")

    Return ChaineSQL.ToString
    End Get
    End Property

    #End Region

    #Region "--- Région Requête SQL Sauvegarder ---"

    Public ReadOnly Property Sauvegarder(ByVal pEnt As TYP_ENT) As String
    Get
    Dim ChaineSQL As New System.Text.StringBuilder

    ChaineSQL.Append("UPDATE ")

    Return ChaineSQL.ToString
    End Get
    End Property

    #End Region

    #Region "--- Région Requête SQL Supprimer ---"

    Public ReadOnly Property Supprimer(ByVal pEnt As TYP_ENT) As String
    Get
    Dim ChaineSQL As New System.Text.StringBuilder

    ChaineSQL.Append("DELETE ")
    ChaineSQL.Append("FROM " & TYP_COM.BDNomTable)
    ChaineSQL.Append(" WHERE ")
    ChaineSQL.Append(TYP_COM.BD100Id & "=")
    ChaineSQL.Append("""" & pEnt.id & """" & ";")

    Return ChaineSQL.ToString

    End Get
    End Property

    #End Region

    #Region "--- Région Requête SQL Lecture ---"

    ' TODO - Note la syntaxe peut changer selon le type de la BD

    Public ReadOnly Property Lecture(ByVal pCri As TYP_CRI) As String
    Get
    Dim ChaineSQL As New System.Text.StringBuilder

    ' TODO : A enlever après déboggage
    pCri.id = String.Empty

    If (String.IsNullOrEmpty(pCri.id)) Then
    ChaineSQL.Append("SELECT ")
    ChaineSQL.Append(TYP_COM.BD100Id)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD110Pseudo)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD120Pass)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD130Email)
    ChaineSQL.Append(", ")
    ChaineSQL.Append(TYP_COM.BD140DtInscr)
    ChaineSQL.Append(" ")
    ChaineSQL.Append(" FROM ")
    ChaineSQL.Append(TYP_COM.BDNomTable)
    ChaineSQL.Append(" ORDER BY ")
    ChaineSQL.Append(TYP_COM.BD100Id)
    Else
    ChaineSQL = New System.Text.StringBuilder
    End If

    Return ChaineSQL.ToString
    End Get
    End Property

    #End Region

    #Region "--- Région Requête SQL Obtenir Numéro Suivant ---"

    Public ReadOnly Property ObtenirNumeroUniqueSuivant() As String
    Get
    Dim ReqSQL As String = "SELECT MAX(" & TYP_COM.BD100Id & ")" & " FROM " & TYP_COM.BDNomTable & ";"
    Return ReqSQL
    End Get
    End Property

    #End Region

    #End Region

    End Class

    Troisième projet - 3 Classes
    Deuxième classe - L'exécution des requêtes



    #Region "--- Importation des classes externes ---"

    Imports System
    Imports System.Data
    Imports System.Data.DataRow

    Imports MySql.Data.MySqlClient

    Imports TYP_ENT = Modele_Entite.ModeleEntite
    Imports TYP_COL = Modele_Entite.ModeleListeTable
    Imports TYP_CRI = Modele_Entite.ModeleCriteres
    Imports TYP_COM = Modele_Commun.Modele_Communaute
    Imports TYP_SQL = Modele_Controleur.Modele_ReqSQL

    #End Region

    Public Class Modele_ExeSQL

    #Region "--- Attributs ---"

    Private zInstanceSQL As TYP_SQL

    Private zConnexion As MySqlConnection

    Private zCheminBD As String = String.Empty

    Private zListeBD As New List(Of String)

    Private zListeTables As New List(Of String)

    #End Region

    #Region "--- Propriétés ---"

    Private Property InstanceSQL() As TYP_SQL
    Get
    Dim oldInst As TYP_SQL = Me.zInstanceSQL
    If (oldInst Is Nothing) Then
    oldInst = New TYP_SQL
    End If
    Return oldInst
    End Get
    Set(ByVal value As TYP_SQL)
    Me.zInstanceSQL = value
    End Set
    End Property

    Private Property CheminBD() As String
    Get
    Return Me.zCheminBD
    End Get
    Set(ByVal value As String)
    Me.zCheminBD = value
    End Set
    End Property

    ''' <summary>
    ''' Instance unique de la classe
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Property Connexion() As MySqlConnection

    Get
    Try
    If (Me.zConnexion Is Nothing) Then
    zConnexion = New MySqlConnection
    End If
    Catch ex As Exception

    End Try

    Return Me.zConnexion
    End Get
    Set(ByVal value As MySqlConnection)
    Me.zConnexion = value
    End Set
    End Property

    Private Property ListeBD() As List(Of String)
    Get
    Return Me.zListeBD
    End Get
    Set(ByVal value As List(Of String))
    Me.zListeBD = value
    End Set
    End Property

    Private Property ListeTables() As List(Of String)
    Get
    Return Me.zListeTables
    End Get
    Set(ByVal value As List(Of String))
    Me.zListeTables = value
    End Set
    End Property

    #End Region

    #Region "--- Méthodes ---"

    #Region "--- Constructeurs ---"

    Public Sub New(ByVal pChemin As String)

    Dim StrTexte As New System.Text.StringBuilder
    CheminBD = pChemin
    Dim Chaine As String = String.Empty

    Try

    ' Parametrage de la chaine de connection
    Chaine = String.Format("server={0};user id={1}; password={2}; database=mysql; pooling=false", "localhost", "root", "kalissi")

    Connexion = New MySqlConnection(Chaine)
    Connexion.Open()

    'GetDatabases()
    'GetTablesListe()
    'AfficheTable()

    Catch ex As Exception
    StrTexte.AppendLine("Erreur de connexion !")
    StrTexte.AppendLine("Modele")
    StrTexte.AppendLine("Constructeur : PC_config_ExeSQL")
    StrTexte.AppendLine(ex.ToString)
    MsgBox(StrTexte.ToString)
    Finally
    Chaine = String.Empty
    End Try

    End Sub

    #End Region

    #Region "--- Procédures ---"

    Private Sub GetDatabases()

    Dim reader As MySqlDataReader
    reader = Nothing

    Dim cmd As New MySqlCommand("SHOW DATABASES", Connexion)
    Try
    reader = cmd.ExecuteReader()
    ListeBD.Clear()

    While (reader.Read())
    ListeBD.Add(reader.GetString(0))
    End While
    Catch ex As MySqlException
    MsgBox("Failed to populate database list: " + ex.Message)
    Finally
    If Not reader Is Nothing Then reader.Close()
    End Try

    End Sub

    Private Sub GetTablesListe()

    Dim reader As MySqlDataReader

    Connexion.ChangeDatabase(ListeBD.Item(1))

    Dim cmd As New MySqlCommand("SHOW TABLES", Connexion)

    Try
    reader = cmd.ExecuteReader()
    ListeTables.Clear()

    While (reader.Read())
    ListeTables.Add(reader.GetString(0))
    End While

    Catch ex As MySqlException
    MsgBox("Failed to populate table list: " + ex.Message)
    Finally
    If Not reader Is Nothing Then reader.Close()
    End Try
    End Sub

    Private Sub AfficheTable()

    Dim NomTables As New System.Text.StringBuilder

    For Each Nom As String In ListeTables
    NomTables.AppendLine(Nom)
    Next

    MsgBox(NomTables.ToString)

    End Sub

    #End Region

    #Region "--- Fonctions ---"

    #Region "--- Région Fonctions de Suppression ---"

    Public Function SupprimerEntite(ByVal pEnt As TYP_ENT) As TYP_ENT

    Dim reqSQL As String = String.Empty
    Dim Indice As Int32 = 0

    Try
    reqSQL = InstanceSQL.Supprimer(pEnt)

    Dim cmd As New MySql.Data.MySqlClient.MySqlCommand(reqSQL, Connexion)

    cmd.CommandText = reqSQL
    Indice = cmd.ExecuteNonQuery()

    pEnt.EstModifier = False
    pEnt.EstNouveau = False
    pEnt.EstSupprimer = False
    pEnt.id = String.Empty

    Catch ex As Exception
    pEnt = Nothing
    Finally
    Connexion.Close()
    End Try

    Return pEnt

    End Function

    #End Region

    #Region "--- Région Fonctions de Lectures ---"

    Public Function Lecture(ByVal pCritere As TYP_CRI) As TYP_COL

    Debug.WriteLine("Entree - PC_config - Lecture")

    Dim MonAdapteur As MySqlDataAdapter
    Dim MonDataTable As DataTable
    Dim MonDataSet As New DataSet
    Dim DataRowLocal As DataRow
    Dim MaCollection As New TYP_COL
    Dim MonEntite As New TYP_ENT

    Try
    Dim reqSQL As String = InstanceSQL.Lecture(pCritere)

    MonAdapteur = New MySqlDataAdapter(reqSQL, Connexion)
    MonAdapteur.Fill(MonDataSet, "membres")
    MonDataTable = MonDataSet.Tables("membres")

    For Each DataRowLocal In MonDataTable.Rows
    MonEntite = CreerInstance(DataRowLocal, pCritere.CheminBD)
    MaCollection.Add(MonEntite)
    Next

    Catch ex As Exception
    Debug.WriteLine("Erreur - PC_config - Lecture")
    MaCollection = Nothing
    Finally
    Connexion.Clone()
    Debug.WriteLine("Sortie - PC_config - Lecture")
    End Try

    Return MaCollection

    End Function

    #End Region

    #Region "--- Région Fonctions de Modifications ---"

    ''' <summary>
    ''' Permet d'enregistrer une collection d'entités selon les critères fournis
    ''' </summary>
    ''' <param name="pEntite"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function InsererEntite(ByVal pEntite As TYP_ENT) As TYP_ENT

    Debug.WriteLine("Entree - PC_Config - InserererEntite")

    Dim reqSQL As String = String.Empty
    Dim Indice As Int32 = 0
    Dim Reponse As Boolean = False

    Try
    reqSQL = InstanceSQL.Insertion(pEntite)

    Dim cmd As New MySqlCommand(reqSQL, Connexion)
    cmd.CommandType = System.Data.CommandType.Text
    cmd.CommandText = reqSQL
    cmd.Connection = Connexion
    Indice = cmd.ExecuteNonQuery()

    pEntite.EstModifier = False
    pEntite.EstNouveau = False
    pEntite.EstSupprimer = False

    Catch ex As Exception
    Debug.WriteLine("Erreur - PC_config - InserererEntite")
    pEntite = Nothing
    Finally
    Connexion.Close()
    Debug.WriteLine("Sortie - PC_config - InserererEntite")
    End Try

    Return pEntite

    End Function

    Public Function SauvegarderEntite(ByVal pEnt As TYP_ENT) As TYP_ENT

    Dim Flag As Boolean = False

    Try
    If (Flag) Then
    pEnt = Nothing
    End If
    Catch ex As Exception

    Finally
    Connexion.Close()
    End Try

    Return pEnt

    End Function

    #End Region

    #Region "--- Région Fonctions de Conversions ---"

    Private Function CreerInstance(ByVal objet As DataRow, ByVal pChemin As String) As TYP_ENT

    Debug.WriteLine("Entree - PC_config : CreerInstance")

    Dim clientEntite As TYP_ENT = New TYP_ENT

    Try

    With clientEntite

    If (objet.Item(TYP_COM.iCol_id_Ind1) IsNot Nothing) Then
    .id = Convert.ToString(objet.Item(TYP_COM.iCol_id_Ind1))
    End If

    If (objet.Item(TYP_COM.iCol_pseudo_Ind2) IsNot Nothing) Then
    .pseudo = Convert.ToString(objet.Item(TYP_COM.iCol_pseudo_Ind2))
    End If

    If (objet.Item(TYP_COM.iCol_pass_Ind3) IsNot Nothing) Then
    .pass = Convert.ToString(objet.Item(TYP_COM.iCol_pass_Ind3))
    End If

    If (objet.Item(TYP_COM.iCol_email_Ind4) IsNot Nothing) Then
    .email = Convert.ToString(objet.Item(TYP_COM.iCol_email_Ind4))
    End If

    If (objet.Item(TYP_COM.iCol_date_inscr_Ind5) IsNot Nothing) Then
    .date_inscription = Convert.ToDateTime(objet.Item(TYP_COM.iCol_date_inscr_Ind5))
    End If

    End With

    Catch ex As Exception
    Debug.WriteLine("Erreur - PC_config : CreerInstance")
    Finally
    Debug.WriteLine("Sortie - PC_config : CreerInstance")
    End Try

    Return clientEntite

    End Function

    #End Region

    #Region "--- Région Fonction Numero Suivant ---"

    Public Function ObtenirNumeroSuivant(ByVal pChemin As String) As String

    Debug.WriteLine("Entree - PC_config - ObtenirNumeroSuivant")

    ' Parametrage des objets de manipulation des données
    Dim MonAdapteur As MySqlDataAdapter
    Dim MonDataTable As DataTable = Nothing
    Dim MonDataSet As New DataSet()
    Dim DataRowLocal As DataRow
    Dim MaCollection As New TYP_COL
    Dim MonEntite As New TYP_ENT

    Dim Compteur As String = String.Empty
    Dim Numero As Int32 = 0

    Try
    Dim reqSQL As String = InstanceSQL.ObtenirNumeroUniqueSuivant

    MonAdapteur = New MySqlDataAdapter(reqSQL, Connexion)
    MonAdapteur.Fill(MonDataSet, "membres")
    MonDataTable = MonDataSet.Tables("membres")

    DataRowLocal = MonDataTable.Rows(0)
    Dim Resultat As String = String.Empty
    If (Not IsDBNull(DataRowLocal.Item(0))) Then
    Compteur = CType(DataRowLocal.Item(0), String)
    If Not (String.IsNullOrEmpty(Compteur)) Then
    If ((Compteur < "100000") And (Compteur > "999999")) Then
    Compteur = "100000"
    End If
    Else
    Compteur = "100000"
    End If
    Else
    Compteur = "100000"
    End If
    Int32.TryParse(Compteur, Numero)
    Numero += 1
    Compteur = Convert.ToString(Numero)

    Catch ex As Exception
    Debug.WriteLine("Erreur - PC_config - ObtenirNumeroSuivant")
    Compteur = String.Empty

    Finally
    Debug.WriteLine("Sortie - PC_config - ObtenirNumeroSuivant")
    Connexion.Close()
    End Try

    Return Compteur.ToString

    End Function

    #End Region

    #End Region

    #End Region

    #Region "--- Commentaires ---"

    #End Region

    End Class

    Troisième projet - 3 Classes
    Troisième classe - Le controleur



    #Region "--- Importation des classes externes ---"

    Imports TYP_ENT = Modele_Entite.ModeleEntite
    Imports TYP_EXE = Modele_Controleur.Modele_ExeSQL
    Imports TYP_COL = Modele_Entite.ModeleListeTable
    Imports TYP_COM = Modele_Commun.Modele_Communaute
    Imports TYP_CRI = Modele_Entite.ModeleCriteres

    #End Region

    Public Class Modele_Control

    #Region "--- Attributs ---"

    Private zCheminBD As String = String.Empty

    Private zInstance As TYP_EXE

    #End Region

    #Region "--- Propriétés ---"

    Private Property CheminBD() As String
    Get
    Return Me.zCheminBD
    End Get
    Set(ByVal value As String)
    Me.zCheminBD = value
    End Set
    End Property

    Private Property InstanceEXE() As TYP_EXE
    Get
    Dim oldInst As TYP_EXE = Me.zInstance
    If (oldInst Is Nothing) Then
    oldInst = New TYP_EXE(CheminBD)
    End If
    Return oldInst
    End Get
    Set(ByVal value As TYP_EXE)
    Me.zInstance = value
    End Set
    End Property

    #End Region

    #Region "--- Méthodes ---"

    #Region "--- Constructeur ---"

    Public Sub New(ByVal pChemin As String)
    CheminBD = pChemin
    End Sub

    #End Region

    #Region "--- Région Fonctions ---"

    Public Function Lecture(ByVal pCrit As TYP_CRI) As TYP_COL

    Dim Liste As TYP_COL = InstanceEXE.Lecture(pCrit)

    Dim Indice As Int32 = 0
    Dim Ent As TYP_ENT

    If (Liste IsNot Nothing) Then
    For Indice = 0 To Liste.Count - 1
    Ent = Liste.Item(Indice)
    Ent.EstModifier = False
    Ent.EstNouveau = False
    Ent.EstSupprimer = False
    Ent = New TYP_ENT
    Next
    End If

    Return Liste

    End Function

    Public Function MAJ_Table(ByVal LaCollection As TYP_COL, ByVal pChemin As String) As TYP_COL

    Debug.WriteLine("Entree - PC_config : MAJ_Table.")

    Dim Indice As Int32 = 0
    Dim Ent As TYP_ENT
    Dim Critere As New TYP_ENT

    'Ajoutés et modifiés
    For Indice = 0 To LaCollection.Count - 1
    Ent = LaCollection.Item(Indice)
    If (Ent.EstModifier) Then
    Ent = InstanceEXE.SauvegarderEntite(Ent)
    Else
    If (Ent.EstNouveau) Then
    Ent = InstanceEXE.InsererEntite(Ent)
    Else
    If (Ent.EstSupprimer) Then
    Ent = InstanceEXE.SupprimerEntite(Ent)
    End If
    End If
    End If
    Next

    Debug.WriteLine("Sortie - PC_config : MAJ_Table.")

    Return LaCollection

    End Function

    Public Function ObtenirNumeroSuivant(ByVal pChemin As String) As String

    Dim Numero As String = InstanceEXE.ObtenirNumeroSuivant(pChemin)

    Return Numero

    End Function

    #End Region

    #End Region

    End Class
    <code>

    Dernier Projet - 2 Formes
    Form Principal

    <code>

    #Region "--- Importations des classes externes ---"

    Imports TYP_CTR = Modele_Controleur.Modele_Control
    Imports TYP_COL = Modele_Entite.ModeleListeTable
    Imports TYP_ENT = Modele_Entite.ModeleEntite
    Imports TYP_CRI = Modele_Entite.ModeleCriteres

    #End Region

    ''' <summary>
    ''' Gestionnaire de données
    ''' </summary>
    ''' <remarks></remarks>
    Public Class PC_config_Form

    #Region "--- Région Attributs ---"

    Private Shadows zInstanceControleur As New TYP_CTR(CheminBD)

    Private Shadows zEntiteLocale As TYP_ENT
    Private Shadows zCollectionLocale As New TYP_COL
    Private Shadows zCheminBD As String = String.Empty

    Private Shadows zOffsetNewMembre As Int32 = 0
    Private Shadows zIndiceModifier As Int32 = 0

    #End Region

    #Region "--- Région Propriétés ---"

    Private Property InstanceCtrl() As TYP_CTR
    Get
    Return Me.zInstanceControleur
    End Get
    Set(ByVal value As TYP_CTR)

    End Set
    End Property

    Private Property EntiteLocale() As TYP_ENT
    Get
    Return Me.zEntiteLocale
    End Get
    Set(ByVal value As TYP_ENT)
    Me.zEntiteLocale = value
    End Set
    End Property

    Private Property CollectionLocale() As TYP_COL
    Get
    Return Me.zCollectionLocale
    End Get
    Set(ByVal value As TYP_COL)
    Me.zCollectionLocale = value
    End Set
    End Property

    Private ReadOnly Property CheminBD() As String
    Get
    Dim Chaine As String = Application.ExecutablePath
    Me.zCheminBD = Chaine.Substring(0, (Chaine.LastIndexOf("\") + 1))
    Return Me.zCheminBD
    End Get
    End Property

    Private Property OffSetNewMembre() As Int32
    Get
    Return Me.zOffsetNewMembre
    End Get
    Set(ByVal value As Int32)
    Me.zOffsetNewMembre = value
    End Set
    End Property

    Private Property IndiceModifier() As Int32
    Get
    Return Me.zIndiceModifier
    End Get
    Set(ByVal value As Int32)
    Me.zIndiceModifier = value
    End Set
    End Property

    #End Region

    #Region "--- Région Méthodes ---"

    #Region "--- Région Constructeur ---"

    ''' <summary>
    ''' Constructeur par défaut
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    ' Cet appel est requis par le Concepteur Windows Form.
    InitializeComponent()

    ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    InitialiseDataGridView()

    End Sub

    #End Region

    #Region "--- Région Évènement ---"

    #Region "--- Région Évènements Boutons ---"

    Private Sub btn_ExecuteDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ExecuteDemo.Click

    Me.Cursor = Cursors.WaitCursor

    Dim EntX As TYP_ENT = NouvelleEntree()

    CollectionLocale.Add(EntX)
    MAJDonnees(CollectionLocale)
    OffSetNewMembre = 0
    AfficheCollection(CollectionLocale)

    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Lecture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Lecture.Click

    Me.Cursor = Cursors.WaitCursor
    Lecture()
    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click

    Me.Cursor = Cursors.WaitCursor
    MAJDonnees(CollectionLocale)
    OffSetNewMembre = 0
    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Modifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Modifier.Click

    Me.Cursor = Cursors.WaitCursor

    Dim Indice As Int32 = 0
    If (CollectionLocale IsNot Nothing) Then
    If (CollectionLocale.Count > 0) Then
    Dim EntY As TYP_ENT
    Indice = Me.DGV_Donnees.CurrentRow.Index
    EntY = CollectionLocale.Item(Indice)
    IndiceModifier = Indice

    Dim FenSec As New Modele_Edition(EntY)

    FenSec.ShowDialog()
    EntY = FenSec.EntiteLoc
    FenSec.Dispose()

    If (EntY IsNot Nothing) Then
    EntY.EstModifier = True
    CollectionLocale.Item(Indice) = EntY
    AfficheCollection(CollectionLocale)
    Me.DGV_Donnees.FirstDisplayedScrollingRowIndex = IndiceModifier
    End If
    End If
    End If

    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Inserer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Inserer.Click

    Me.Cursor = Cursors.WaitCursor

    Dim Indice As Int32 = 0
    Dim EntY As New TYP_ENT
    Indice = Me.DGV_Donnees.RowCount
    EntY.id = ObtenirNumeroSuivant()
    Dim FenSec As New Modele_Edition(EntY)

    FenSec.ShowDialog()
    EntY = FenSec.EntiteLoc
    FenSec.Dispose()

    If (EntY IsNot Nothing) Then
    EntY.EstNouveau = True
    CollectionLocale.Item(Indice) = EntY
    AfficheCollection(CollectionLocale)
    End If

    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Supprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Supprimer.Click

    Me.Cursor = Cursors.WaitCursor

    If (CollectionLocale IsNot Nothing) Then
    If (CollectionLocale.Count > 0) Then
    Dim Indice As Int32 = Me.DGV_Donnees.CurrentRow.Index
    Dim EntX As TYP_ENT = CollectionLocale.Item(Indice)
    EntX.EstSupprimer = True
    Dim Col_Tmp As New TYP_COL
    Col_Tmp.Add(EntX)
    Dim InstanceCtrl As New TYP_CTR(CheminBD)
    Col_Tmp = InstanceCtrl.MAJ_Table(Col_Tmp, CheminBD)
    EntX = Col_Tmp.Item(0)
    If (String.IsNullOrEmpty(EntX.id)) Then
    Lecture()
    End If

    End If
    End If

    Me.Cursor = Cursors.Default

    End Sub

    Private Sub btn_Quitter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Quitter.Click
    Me.Close()
    End Sub

    #End Region

    #End Region

    #Region "--- Région Procédures ---"

    Public Sub Lecture()

    Dim ListeElements As New TYP_COL
    Dim Critere As New TYP_CRI(CheminBD)

    InitialiseDataGridView()
    CollectionLocale = InstanceCtrl.Lecture(Critere)

    If (CollectionLocale IsNot Nothing) Then
    If (CollectionLocale.Count > 0) Then
    AfficheCollection(CollectionLocale)
    Else
    MessageBox.Show("Aucune donnée", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
    Else
    MessageBox.Show("Une erreur est survenue !", "Fatale", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

    Me.DGV_Donnees.Refresh()

    End Sub

    Public Sub MAJDonnees(ByVal pCol As TYP_COL)

    Dim Ctrl As New TYP_CTR(CheminBD)

    Dim Col_Tmp As TYP_COL = Ctrl.MAJ_Table(pCol, CheminBD)
    Lecture()

    If (CollectionLocale IsNot Nothing) Then
    If (CollectionLocale.Count > 0) Then
    AfficheCollection(CollectionLocale)
    End If
    End If

    End Sub

    Private Sub AfficheCollection(ByVal pCol As TYP_COL)

    If (pCol IsNot Nothing) Then
    If (pCol.Count > 0) Then
    InitialiseDataGridView()
    Dim Indice As Int32 = pCol.Count - 1
    For Boucle As Int32 = 0 To Indice
    Me.DGV_Donnees.Rows.Add()
    Me.DGV_Donnees.Rows(Boucle).Cells(0).Value = pCol.Item(Boucle).id
    Me.DGV_Donnees.Rows(Boucle).Cells(1).Value = pCol.Item(Boucle).pseudo
    Me.DGV_Donnees.Rows(Boucle).Cells(2).Value = pCol.Item(Boucle).pass
    Me.DGV_Donnees.Rows(Boucle).Cells(3).Value = pCol.Item(Boucle).email
    Me.DGV_Donnees.Rows(Boucle).Cells(4).Value = pCol.Item(Boucle).date_inscription.ToShortDateString
    Next
    End If
    End If

    End Sub

    Private Shadows Function InitialiseDataGridView() As Boolean

    Me.DGV_Donnees.Rows.Clear()

    'Permet de sélectionner toute une ligne
    Me.DGV_Donnees.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    'Interdit la sélection de plusieurs projet
    Me.DGV_Donnees.MultiSelect = False

    Me.DGV_Donnees.ColumnCount = 8

    Me.DGV_Donnees.Columns(0).HeaderText = "id"
    Me.DGV_Donnees.Columns(0).Width = 100

    Me.DGV_Donnees.Columns(1).HeaderText = "pseudo"
    Me.DGV_Donnees.Columns(1).Width = 100

    Me.DGV_Donnees.Columns(2).HeaderText = "pass"
    Me.DGV_Donnees.Columns(2).Width = 100

    Me.DGV_Donnees.Columns(3).HeaderText = "email"
    Me.DGV_Donnees.Columns(3).Width = 100

    Me.DGV_Donnees.Columns(4).HeaderText = "dt inscr"
    Me.DGV_Donnees.Columns(4).Width = 100
    Me.DGV_Donnees.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

    Me.DGV_Donnees.Columns(5).HeaderText = "est nouv"
    Me.DGV_Donnees.Columns(5).Width = 50
    Me.DGV_Donnees.Columns(5).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    Me.DGV_Donnees.Columns(5).Visible = False

    Me.DGV_Donnees.Columns(6).HeaderText = "est mod"
    Me.DGV_Donnees.Columns(6).Width = 50
    Me.DGV_Donnees.Columns(6).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    Me.DGV_Donnees.Columns(6).Visible = False

    Me.DGV_Donnees.Columns(7).HeaderText = "est supp"
    Me.DGV_Donnees.Columns(7).Width = 50
    Me.DGV_Donnees.Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    Me.DGV_Donnees.Columns(7).Visible = False

    Return True

    End Function

    #End Region

    #Region "--- Région Fonctions ---"

    Private Function NouvelleEntree() As TYP_ENT

    Dim EntX As New TYP_ENT

    EntX.id = ObtenirNumeroSuivant()
    EntX.pseudo = "voyageur"
    EntX.pass = "75cg56914e78abfkd59b60209cbc46d528f7d781"
    EntX.email = "voyageur@hotmail.fr"
    EntX.date_inscription = Date.Parse("2012-01-01")
    EntX.EstNouveau = True

    Return EntX

    End Function

    Private Function ObtenirNumeroSuivant() As String

    Dim Numero As String = String.Empty
    Dim InstanceCtrl As New TYP_CTR(CheminBD)
    Numero = InstanceCtrl.ObtenirNumeroSuivant(CheminBD)
    Dim Compteur As Int32 = Int32.Parse(Numero)
    Compteur += OffSetNewMembre
    OffSetNewMembre += 1

    Numero = Compteur.ToString

    Return Numero

    End Function

    #End Region

    #End Region

    End Class

    Dernier Projet - 2 Formes
    Form Secondaire



    #Region "--- Importation des classes extrenes ---"

    Imports TYP_ENT = Modele_Entite.ModeleEntite

    #End Region

    ''' <summary>
    '''
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Modele_Edition

    #Region "--- Région Constantes ---"
    #End Region

    #Region "--- Région Attributs ---"

    Private Shadows zEntiteLoc As TYP_ENT

    #End Region

    #Region "--- Région Propriétés ---"

    Public Property EntiteLoc() As TYP_ENT
    Get
    Return Me.zEntiteLoc
    End Get
    Set(ByVal value As TYP_ENT)
    Me.zEntiteLoc = value
    End Set
    End Property

    #End Region

    #Region "--- Région Méthodes ---"

    #Region "--- Région Constructeurs ---"

    Public Sub New(ByVal pEnt As TYP_ENT)

    ' Cet appel est requis par le Concepteur Windows Form.
    InitializeComponent()

    ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    EntiteLoc = pEnt

    End Sub

    #End Region

    #Region "--- Région Évènements ---"

    #Region "--- Région Évènements Formulaire ---"

    Private Sub PC_config_Edition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AfficheEntite()
    End Sub

    #End Region

    #Region "--- Région Évènements Boutons ---"

    Private Sub btn_Annuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Annuler.Click
    EntiteLoc = Nothing
    Me.Close()
    End Sub

    Private Sub btn_MAJ_Entite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_MAJ_Entite.Click
    RecupereEntite()
    Me.Close()
    End Sub

    #End Region

    #End Region

    #Region "--- Région Procédures ---"

    Private Sub RecupereEntite()

    EntiteLoc.id = tbx_id.Text
    EntiteLoc.pseudo = tbx_pseudo.Text
    EntiteLoc.pass = tbx_pass.Text
    EntiteLoc.email = tbx_courriel.Text
    Dim LaDate As Date
    If (Date.TryParse(tbx_dt_inscr.Text, LaDate)) Then
    EntiteLoc.date_inscription = LaDate
    End If

    End Sub

    Private Sub AfficheEntite()

    Me.tbx_id.Text = EntiteLoc.id
    Me.tbx_pseudo.Text = EntiteLoc.pseudo
    Me.tbx_pass.Text = EntiteLoc.pass
    Me.tbx_courriel.Text = EntiteLoc.email
    If (EntiteLoc.date_inscription > Date.Parse("1950-01-01")) Then
    Me.tbx_dt_inscr.Text = EntiteLoc.date_inscription.ToShortDateString
    End If

    End Sub

    #End Region

    #Region "--- Région Fonctions ---"
    #End Region

    #End Region

    End Class

    En résumé, une solution, 4 projets, 7 Classes, 2 Form.

    En pratique, tu ne travaille jamais directement sur ta(es) grille(s),
    tu travaille dans la(es) liste(s) fortement(s) typé(s), et tu affiche le contenu de la(es) liste(s) dans la(es) grille(s).

    Regarde du côté des méthodes LinQ pour ordonné tes listes de façon à
    faire concorder l'index de ta grille avec l'Index de la liste fortement typé.
    Ainsi, tu ne récupère pas tes données de la grille mais bien de la liste fortement typé.

    Voilà, je ne peux t'aider davantage, comme spécifié je ne code pas en CSharp,
    mais j'ai déjà converti du CSharp en VB à l'aide de convertisseur sur la toile.

    Et j'ai réalisé, que ma requète UPDATE n'est pas complète ...

    K
    0
  4. Ilovedev Messages postés 19 Statut Membre
     
    Merci beaucoup pour ton post je vais regarder tout ça et m'en inspirer... :o)
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. Kalissi Messages postés 221 Statut Membre 20
     
    re:

    Prend note que j'ai produit ce modèle, il y a près d'un ans, et qu'aujourd'hui
    j'utilise de plus en plus les méthodes LinQ, de plus certaines portions de réinitialisation ne sont pas optimiser.


    Public Function Lecture(ByVal pCrit As TYP_CRI) As TYP_COL

    Dim Liste As TYP_COL = InstanceEXE.Lecture(pCrit)
    Dim Indice As Int32 = 0
    Dim Ent As TYP_ENT

    If (Liste IsNot Nothing) Then
    For Indice = 0 To Liste.Count - 1
    Ent = Liste.Item(Indice)
    Ent.EstModifier = False
    Ent.EstNouveau = False
    Ent.EstSupprimer = False
    Ent = New TYP_ENT
    Next
    End If

    Return Liste
    End Function

    Ici, Ent n'est pas réintroduit dans la liste, donc le code entre le If ... End If est
    inutile.

    Au lieu de l'instruction :

    Ent = New TYP_ENT

    On devrait avoir :

    Liste.Item(Indice) = Ent

    Lors de la lecture, ce sont les propriétés EstModifier, EstNouveau et EstSupprimer
    qui doivent être initialisés à False.

    K
    0
  7. Ilovedev Messages postés 19 Statut Membre
     
    Encore merci pour ton aide...bonne journée :o)
    0