Bonjour,

J'ai une GV qui est chargée à partir d'une BDD.
Lorsque je clique sur le bouton Edit d'une ligne de mon GV, j'arrive à modifier les champs mais lorsque je clique sur Update, j'ai une erreur OutOfRange sur la ligne int myID = Convert.ToInt32(gv_db.DataKeys[e.RowIndex].Value);

Code ASP

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
 
<asp:GridView ID="gv_db" runat="server" AutoGenerateColumns="False" DataKeyNames="id" OnRowEditing="gv_db_RowEditing" OnRowUpdating="gv_db_RowUpdating" OnRowCancelingEdit="gv_db_RowCanceling" CssClass="gridView" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" HorizontalAlign="Left">
                    <HeaderStyle CssClass="header"></HeaderStyle>
                    <RowStyle CssClass="rows" Wrap="False"></RowStyle>
                    <Columns>
                    <%--<asp:CommandField ShowEditButton="True" />--%>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="LkB1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID="LB2" runat="server" CommandName="Update">Update</asp:LinkButton>
                                <asp:LinkButton ID="LB3" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
                            </EditItemTemplate>
                        </asp:TemplateField>
 
                        <asp:TemplateField HeaderText="id2">
                            <ItemTemplate>
                                <asp:Label ID="lbl_id2" runat="server" Text='<%# Eval("id2") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="tb_id2" runat="server" Text='<%# Eval("id2") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
 
                        <asp:TemplateField HeaderText="BU">
                            <ItemTemplate>
                                <asp:Label ID="lbl_BU" runat="server" Text='<%# Eval("BU") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="tb_BU" runat="server" Text='<%# Eval("BU") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>  
...
Code C#
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
protected void gv_db_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        // find student id of edit row
        //string id = gv_db.DataKeys[e.RowIndex].Value.ToString();
        int myID = Convert.ToInt32(gv_db.DataKeys[e.RowIndex].Value);
        // find updated values for update

        TextBox id2 = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_id2");
        TextBox BU = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_BU");
        TextBox Country = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_Country");
        TextBox Alpha2 = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_Alpha2");
        TextBox Digital = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_Digital");
        TextBox City = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_City");
        TextBox State = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_State");
        TextBox cp = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_cp");
        TextBox UPN = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_UPN");
        TextBox displayName = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_displayName");
        TextBox o365 = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_o365");
        TextBox vpn = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_vpn");
        TextBox internet = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_internet");
        TextBox OU = (TextBox)gv_db.Rows[e.RowIndex].FindControl("txt_OU");


        DB.UpdateDBRef("SQL_UPDATE_1", myID, id2.Text, BU.Text, Country.Text, Alpha2.Text, Digital.Text, City.Text, State.Text, cp.Text, UPN.Text, displayName.Text, o365.Text, vpn.Text, internet.Text, OU.Text);

        gv_db.EditIndex = -1;
        gv_db.DataSource = DB.GetDBRef();
        gv_db.DataBind();
    }
Une idée ?