更新 GridView ASP NET 中的行不起作用



我是新的ASP.NET开发人员,并试图在GridView中更新一行。我尝试了许多解决方案,但它不起作用。当我更新一行时,不是错误消息,而是不考虑更新。

<asp:GridView ID="grd_quest" Visible = "False" runat="server" CellPadding="4" 
                  DataSourceID="ERP_questionn" ForeColor="#333333" GridLines="None" 
                  AutoGenerateColumns="False" DataKeyNames="QUE_id">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:CommandField ShowEditButton="true" />  
        <asp:BoundField DataField="QUE_id" HeaderText="ID" ReadOnly="True" 
            SortExpression="QUE_id" ItemStyle-Width="10%" />
        <asp:BoundField DataField="QUE_libelle" HeaderText="Libelle" 
            SortExpression="QUE_libelle" />
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#E9E7E2" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingCellStyle BackColor="#FFFDF8" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="ERP_questionn" runat="server" 
      ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>" 
      SelectCommand="" 
      UpdateCommand=
      "UPDATE [TR_QUESTION] SET [QUE_libelle] = @QUE_libelle WHERE [QUE_id] = @QUE_id">
    <UpdateParameters>
        <asp:Parameter Name="QUE_libelle" Type="String" />
        <asp:Parameter Name="QUE_id" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

选择命令不是在这里写的,因为它会在其他列表的功能上更改,因此我在C#中写下SelectCommand。我希望这对您来说很清楚

[编辑]

我在C#中尝试了其他解决方案:

protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection sqlConnection1 = new SqlConnection(connectionString);
        int id = Convert.ToInt32(grd_quest.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row = (GridViewRow)grd_quest.Rows[e.RowIndex];
        //TextBox txtname=(TextBox)gr.cell[].control[];  
        TextBox textadd = (TextBox)row.Cells[2].Controls[0];
        //TextBox textadd = (TextBox)row.FindControl("txtadd");  
        //TextBox textc = (TextBox)row.FindControl("txtc");  
        grd_quest.EditIndex = -1;
        sqlConnection1.Open();
        //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);  
        SqlCommand cmd = new SqlCommand("UPDATE TR_QUESTION SET QUE_libelle = @p_libelle where QUE_id = @p_id ", sqlConnection1);
        cmd.Parameters.AddWithValue("@p_id", id);
        cmd.Parameters.AddWithValue("@p_libelle", textadd.Text);         
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();
        grd_quest.DataBind();
    }

但它也行不通,我有一个错误消息,他说:

除非指定了UpdateCommand,否则数据源'erp_questionn'不支持更新。我有尝试:updateCommand =",但它不起作用

[编辑新尝试]

    protected void grd_quest_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = grd_quest.DataKeys[e.RowIndex].Value.ToString();
        string libelle = ((TextBox)grd_quest.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        ERP_questionn.UpdateParameters["QUE_id"].DefaultValue = id;
        ERP_questionn.UpdateParameters["QUE_libelle"].DefaultValue = libelle;
        ERP_questionn.Update();
        //ERP_questionn.SelectCommand = DropDownList1.SelectedValue;
    }

当我更新一行时,不是错误消息,而是不考虑更新。

[编辑]

也许可以有所帮助,以在我的GridView中显示我的数据:

protected void lst_facteur_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lst_pos.SelectedValue == "1")
        {
            ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreUsine IS NOT NULL";
        }
        else if (lst_pos.SelectedValue == "2")
        {
            ERP_questionn.SelectCommand = "select * FROM TR_QUESTION WHERE FAC_id =" + lst_facteur.SelectedValue + " AND QUE_ordreBureau IS NOT NULL";
        }
        grd_quest.DataBind();
        grd_quest.Visible = true;
        txt_question.Visible = true;
        btn_add_question.Visible = true;

我总是有更新问题...

"UPDATE [TR_QUESTION] SET [QUE_libelle] =? WHERE [QUE_id] =?"

以及此外,您尚未在GridView上设置更新命令事件。

onRowupDated =" cunitustridview_rowupdated"

您必须在> html代码中设置SelectCommand = "SELECT * FROM [TR_QUESTION]"

<asp:GridView ID="grd_quest" runat="server" AutoGenerateColumns="False" DataKeyNames="QUE_id" DataSourceID="ERP_questionn">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="QUE_id" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="QUE_id" />
        <asp:BoundField DataField="QUE_libelle" HeaderText="Libelle" SortExpression="QUE_libelle" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="ERP_questionn" runat="server" 
      ConnectionString="<%$ ConnectionStrings:ERPConnectionString %>" 
      SelectCommand = "SELECT * FROM [TR_QUESTION]" 
      UpdateCommand = 
      "UPDATE [TR_QUESTION] SET [QUE_libelle] = @QUE_libelle WHERE [QUE_id] = @QUE_id">
</asp:SqlDataSource>

最新更新