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

ASP.NET Discussion :

[2.0][c#] Liaison gridview / formview


Sujet :

ASP.NET

  1. #1
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut [ASP.net][c#] Liaison gridview sur plusieurs formview - nouvelle question :(
    Bonjour,

    J'aimerais pouvoir relier un grid view à un formview. Je m'explique, j'ai un gridview avec possibilité de séléctionner un ligne.

    J'ai uen liste de personnes et quand je clique sur le nom j'aimerais qu'un formview affiche les détails de cette personne.

    Pouvez-vous m'aider ?

    Merki

  2. #2
    Membre Expert Avatar de bossun
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 359
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : Suisse

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 359
    Par défaut
    t dois juste déffinir que ton paramètre d'entrée, du controle qui aliment le formview (SQLDataSource, ou autre), sera le DataGrid....

  3. #3
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut
    Citation Envoyé par bossun
    t dois juste déffinir que ton paramètre d'entrée, du controle qui aliment le formview (SQLDataSource, ou autre), sera le DataGrid....
    D'accord, mais je ne sais pas comment faire ce que tu demandes

    je dois faire des changements ici ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {
     
        }

  4. #4
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut
    J'ai essayé de suivre le tutoriel 4 du coach asp sur le site de microsoft. Je me suis inspiré des solutions et voici le code que j'ai :

    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
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     
       <div class="content"> &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="idsal">
               <Columns>
                   <asp:CommandField ShowSelectButton="True" />
                   <asp:BoundField DataField="nom" HeaderText="nom" SortExpression="nom" />
                   <asp:BoundField DataField="prenom" HeaderText="prenom" SortExpression="prenom" />
                   <asp:BoundField DataField="adresse1" HeaderText="adresse1" SortExpression="adresse1" />
                   <asp:BoundField DataField="adresse2" HeaderText="adresse2" SortExpression="adresse2" />
                   <asp:BoundField DataField="codepost" HeaderText="codepost" SortExpression="codepost" />
                   <asp:BoundField DataField="ville" HeaderText="ville" SortExpression="ville" />
                   <asp:BoundField DataField="telfixe" HeaderText="telfixe" SortExpression="telfixe" />
                   <asp:BoundField DataField="telport" HeaderText="telport" SortExpression="telport" />
                   <asp:BoundField DataField="telpro" HeaderText="telpro" SortExpression="telpro" />
                   <asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />
               </Columns>
           </asp:GridView>
           <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranet2ConnectionString %>"
               SelectCommand="SELECT [idsalarie] [nom], [prenom], [adresse1], [adresse2], [codepost], [ville], [telfixe], [telport], [telpro], [mail] FROM [salarie]">
           </asp:SqlDataSource>
     
     
           <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:intranet2ConnectionString %>"
               SelectCommand="SELECT [adresse1], [telport], [telpro] FROM [salarie] where ([idsalarie]=@idsal)">
               <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="idsal" 
                                     PropertyName="SelectedValue"
                                     Type="Int32" />
            </SelectParameters>
     
               </asp:SqlDataSource>
           <br />
           <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2"
               Height="50px" Width="125px" OnPageIndexChanging="DetailsView1_PageIndexChanging">
               <Fields>
                   <asp:TemplateField HeaderText="adresse1" SortExpression="adresse1">
                       <EditItemTemplate>
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("adresse1") %>'></asp:TextBox>
                       </EditItemTemplate>
                       <InsertItemTemplate>
                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("adresse1") %>'></asp:TextBox>
                       </InsertItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label1" runat="server" Text='<%# Eval("adresse1") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="telport" SortExpression="telport">
                       <EditItemTemplate>
                           <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("telport") %>'></asp:TextBox>
                       </EditItemTemplate>
                       <InsertItemTemplate>
                           <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("telport") %>'></asp:TextBox>
                       </InsertItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label2" runat="server" Text='<%# Eval("telport") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:TemplateField HeaderText="telpro" SortExpression="telpro">
                       <EditItemTemplate>
                           <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("telpro") %>'></asp:TextBox>
                       </EditItemTemplate>
                       <InsertItemTemplate>
                           <asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("telpro") %>'></asp:TextBox>
                       </InsertItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="Label3" runat="server" Text='<%# Eval("telpro") %>'></asp:Label>
                       </ItemTemplate>
                   </asp:TemplateField>
               </Fields>
           </asp:DetailsView>
           &nbsp;
     
       </div>
        <br />
        <br />
        <a href="ajoutsalarie.aspx">Nouveau ?</a> <a href="lieprojet.aspx">Affection à un projet</a>
     
    </asp:Content>
    ça ne fonctionne pas, il ne connaît pas "idsal"...

  5. #5
    Membre Expert Avatar de bossun
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 359
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : Suisse

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 359
    Par défaut
    remplace ça ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="idsal">
    par ça


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="idsalarie">

    c'est normal qu'il reconnaisse pas.. idsal n'existe pas dans le datagrid

    DataKeyNames doit contenir un champ qui te permet d'identifier un enregistrement.. donc un champ renvoyé par le SQLDataSource...

  6. #6
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut
    Citation Envoyé par bossun
    remplace ça ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="idsal">
    par ça


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="idsalarie">

    c'est normal qu'il reconnaisse pas.. idsal n'existe pas dans le datagrid

    DataKeyNames doit contenir un champ qui te permet d'identifier un enregistrement.. donc un champ renvoyé par le SQLDataSource...
    Oups, merci pour l'aide

  7. #7
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut
    j'aurais une autre question en fait.

    Maintenant que j'arrive à lié deux gridview ensemble, j'aimerais en lié 3 voir 4.

    Comment rajouter plus d'un DataKeynames?

  8. #8
    Membre Expert Avatar de bossun
    Profil pro
    Inscrit en
    Novembre 2002
    Messages
    1 359
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : Suisse

    Informations forums :
    Inscription : Novembre 2002
    Messages : 1 359
    Par défaut
    tu voudrais lier 3 ou 4 formviews à la gridview???

    ben t'as pas besoi de créer d'autres DataKeynames.. tu n'as qu'à repeter l'0pération pour ajouter d'autres fromviews...

  9. #9
    Membre confirmé
    Inscrit en
    Février 2006
    Messages
    174
    Détails du profil
    Informations forums :
    Inscription : Février 2006
    Messages : 174
    Par défaut
    Citation Envoyé par bossun
    tu voudrais lier 3 ou 4 formviews à la gridview???

    ben t'as pas besoi de créer d'autres DataKeynames.. tu n'as qu'à repeter l'0pération pour ajouter d'autres fromviews...

    Ok.

    Bon j'ai un autre problème, jsutement avec un formview lié à un autre. La mise à jour ne fonctionne pas...

    voici un aperçu d'écran :




    et le 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
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3"
            ForeColor="#333333" GridLines="None" DataKeyNames="idrole">
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" />
                <asp:BoundField DataField="nom" HeaderText="nom" SortExpression="nom" />
                <asp:BoundField DataField="prenom" HeaderText="prenom" SortExpression="prenom" />
                <asp:BoundField DataField="adresse1" HeaderText="adresse1" SortExpression="adresse1" />
                <asp:BoundField DataField="adresse2" HeaderText="adresse2" SortExpression="adresse2" />
                <asp:BoundField DataField="codepost" HeaderText="codepost" SortExpression="codepost" />
                <asp:BoundField DataField="ville" HeaderText="ville" SortExpression="ville" />
                <asp:BoundField DataField="telfixe" HeaderText="telfixe" SortExpression="telfixe" />
                <asp:BoundField DataField="telport" HeaderText="telport" SortExpression="telport" />
                <asp:BoundField DataField="telpro" HeaderText="telpro" SortExpression="telpro" />
                <asp:BoundField DataField="mail" HeaderText="mail" SortExpression="mail" />
     
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConflictDetection="CompareAllValues"
            ConnectionString="<%$ ConnectionStrings:intranet2ConnectionString %>" DeleteCommand="DELETE FROM [salarie] WHERE [idsalarie] = @original_idsalarie AND [nom] = @original_nom AND [prenom] = @original_prenom AND [adresse1] = @original_adresse1 AND [adresse2] = @original_adresse2 AND [codepost] = @original_codepost AND [ville] = @original_ville AND [telfixe] = @original_telfixe AND [telport] = @original_telport AND [telpro] = @original_telpro AND [mail] = @original_mail AND [idrole] = @original_idrole"
            InsertCommand="INSERT INTO [salarie] ([nom], [prenom], [adresse1], [adresse2], [codepost], [ville], [telfixe], [telport], [telpro], [mail], [idrole]) VALUES (@nom, @prenom, @adresse1, @adresse2, @codepost, @ville, @telfixe, @telport, @telpro, @mail, @idrole)"
            OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [nom], [idsalarie], [prenom], [adresse1], [adresse2], [codepost], [ville], [telfixe], [telport], [telpro], [mail], [idrole] FROM [salarie]"
            UpdateCommand="UPDATE [salarie] SET [nom] = @nom, [prenom] = @prenom, [adresse1] = @adresse1, [adresse2] = @adresse2, [codepost] = @codepost, [ville] = @ville, [telfixe] = @telfixe, [telport] = @telport, [telpro] = @telpro, [mail] = @mail, [idrole] = @idrole WHERE [idsalarie] = @original_idsalarie AND [nom] = @original_nom AND [prenom] = @original_prenom AND [adresse1] = @original_adresse1 AND [adresse2] = @original_adresse2 AND [codepost] = @original_codepost AND [ville] = @original_ville AND [telfixe] = @original_telfixe AND [telport] = @original_telport AND [telpro] = @original_telpro AND [mail] = @original_mail AND [idrole] = @original_idrole">
            <DeleteParameters>
                <asp:Parameter Name="original_idsalarie" Type="Int32" />
                <asp:Parameter Name="original_nom" Type="String" />
                <asp:Parameter Name="original_prenom" Type="String" />
                <asp:Parameter Name="original_adresse1" Type="String" />
                <asp:Parameter Name="original_adresse2" Type="String" />
                <asp:Parameter Name="original_codepost" Type="String" />
                <asp:Parameter Name="original_ville" Type="String" />
                <asp:Parameter Name="original_telfixe" Type="String" />
                <asp:Parameter Name="original_telport" Type="String" />
                <asp:Parameter Name="original_telpro" Type="String" />
                <asp:Parameter Name="original_mail" Type="String" />
                <asp:Parameter Name="original_idrole" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="nom" Type="String" />
                <asp:Parameter Name="prenom" Type="String" />
                <asp:Parameter Name="adresse1" Type="String" />
                <asp:Parameter Name="adresse2" Type="String" />
                <asp:Parameter Name="codepost" Type="String" />
                <asp:Parameter Name="ville" Type="String" />
                <asp:Parameter Name="telfixe" Type="String" />
                <asp:Parameter Name="telport" Type="String" />
                <asp:Parameter Name="telpro" Type="String" />
                <asp:Parameter Name="mail" Type="String" />
                <asp:Parameter Name="idrole" Type="Int32" />
                <asp:Parameter Name="original_idsalarie" Type="Int32" />
                <asp:Parameter Name="original_nom" Type="String" />
                <asp:Parameter Name="original_prenom" Type="String" />
                <asp:Parameter Name="original_adresse1" Type="String" />
                <asp:Parameter Name="original_adresse2" Type="String" />
                <asp:Parameter Name="original_codepost" Type="String" />
                <asp:Parameter Name="original_ville" Type="String" />
                <asp:Parameter Name="original_telfixe" Type="String" />
                <asp:Parameter Name="original_telport" Type="String" />
                <asp:Parameter Name="original_telpro" Type="String" />
                <asp:Parameter Name="original_mail" Type="String" />
                <asp:Parameter Name="original_idrole" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="nom" Type="String" />
                <asp:Parameter Name="prenom" Type="String" />
                <asp:Parameter Name="adresse1" Type="String" />
                <asp:Parameter Name="adresse2" Type="String" />
                <asp:Parameter Name="codepost" Type="String" />
                <asp:Parameter Name="ville" Type="String" />
                <asp:Parameter Name="telfixe" Type="String" />
                <asp:Parameter Name="telport" Type="String" />
                <asp:Parameter Name="telpro" Type="String" />
                <asp:Parameter Name="mail" Type="String" />
                <asp:Parameter Name="idrole" Type="Int32" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="Rôle du salarié" SortExpression="libel_role">
                    <EditItemTemplate>
     
     
                         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranet2ConnectionString %>"
                            SelectCommand="SELECT libel_role FROM [role]"></asp:SqlDataSource>
                        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="libel_role"
                            DataValueField="libel_role" SelectedValue='<%# Bind("libel_role") %>'>
                        </asp:DropDownList>
     
                    </EditItemTemplate>
     
                    <ItemTemplate>
     
     
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("libel_role") %>'></asp:Label>
     
     
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:intranet2ConnectionString %>"
            SelectCommand="SELECT idrole, libel_role FROM [role] where ([idrole]=@idrole)" DeleteCommand="DELETE FROM [role] WHERE [idrole] = @idrole" InsertCommand="INSERT INTO [role] ([libel_role]) VALUES (@libel_role)" UpdateCommand="UPDATE [salarie] SET [idrole] = idrole WHERE [idrole] = @idrole">
            <DeleteParameters>
                <asp:Parameter Name="idrole" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="libel_role" Type="String" />
                <asp:Parameter Name="idrole" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="libel_role" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="idrole" 
                                     PropertyName="SelectedValue"
                                     Type="Int32" />
            </SelectParameters>
            </asp:SqlDataSource>
    </asp:Content>

    l'udpate du gridview 2 ne fonctionne pas, pourtant, la commande update a l'air correcte. Il ne met pas à jour le champ et laisse "chef de projet". idrole la clef primaire d'une table role qui est aussi une clef étrangère de la table salarié.

    D'où cela peut-il venir?

Discussions similaires

  1. Réponses: 1
    Dernier message: 10/03/2010, 14h05
  2. Formview en popup pour éditer une ligne d'un Gridview
    Par sovitec dans le forum ASP.NET
    Réponses: 6
    Dernier message: 28/09/2009, 12h52
  3. Liaison objet formview .NET2.0
    Par clodo13 dans le forum ASP.NET
    Réponses: 1
    Dernier message: 28/08/2009, 15h14
  4. Réponses: 4
    Dernier message: 03/01/2007, 16h47
  5. Réponses: 3
    Dernier message: 24/08/2006, 23h24

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