在GridView ASP NET中更新一行并不起作用

时间:2017-09-13 07:01:58

标签: c# asp.net gridview sqldatasource

我是新的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>

这里没有写入select命令,因为它改变了其他列表的功能,所以我在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();

    }

但它也不起作用,我有一条错误信息说:

数据源&#39; ERP_questionn&#39;除非指定UpdateCommand,否则不支持更新。我试过:UpdateCommand =&#34;&#34;但它也不起作用

[编辑新尝试]

    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;

我一直都有更新问题......

2 个答案:

答案 0 :(得分:0)

您还必须在 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>

答案 1 :(得分:0)

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

除此之外,您还没有在gridview上设置更新命令事件。

引发OnRowUpdated =&#34; CustomersGridView_RowUpdated&#34;