ASP.NET在GridView中配置更新按钮

时间:2011-11-25 03:50:56

标签: c# asp.net visual-studio gridview

我在VS2005上使用C#ASP.NET。

我有一个gridview表,但是当我右键单击智能选项卡时,它没有选择启用编辑

因此,我使用以下代码手动添加了编辑按钮:

AutoGenerateEditButton="True"

编辑按钮已成功出现在我的gridview上,如下所示: enter image description here

当我点击编辑按钮时,页面会刷新,现在可以编辑行: enter image description here

然而,当我按下更新按钮时,我遇到了错误:

Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.

enter image description here http://i.stack.imgur.com/W97K0.png

我不知道如何输入或配置UpdateCommand,因为我没有看到Update按钮的任何背景代码。

需要经验丰富的帮助。提前谢谢。


编辑:在SqlDataSource1中添加了INSERT查询,但是当我按下Update按钮时仍然遇到同样的错误。

4 个答案:

答案 0 :(得分:3)

您需要重新配置SqlDataSource1控件,您可以添加对INSERTDELETEUPDATE以及SELECT的支持。

查看this教程。

答案 1 :(得分:1)

在为gridview配置select语句时配置sqldatasource时,有一个选项为“advanced”。单击它,然后单击“生成更新,插入nad删除语句”。

答案 2 :(得分:0)

例如,试试这个......

  1. 首先创建一个处理更新记录的方法。

    private void UpdateOrAddNewRecord(string parametervalue1, string parametervalue2)
        {
            using (openconn())
            {
                string sqlStatement = string.Empty;
                sqlStatement = "Update TableName set Name1 =@Name1 where Name2@Name2";
    
    
                try
                {
                //  SqlCommand cmd = new SqlCommand("storedprocedureName", con);
                  //cmd.CommandType = CommandType.StoredProcedure;
    
                    SqlCommand cmd = new SqlCommand(sqlStatement, con);
                    cmd.Parameters.AddWithValue("Name2", parametervalue2);
                    cmd.Parameters.AddWithValue("@Name1",parametervalue1);
    
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
    
                }
    
    
                catch (System.Data.SqlClient.SqlException ex)
                {
    
                    string msg = "Insert/Update Error:";
    
                    msg += ex.Message;
    
                    throw new Exception(msg);
    
    
                }
    
                finally
                {
    
                    closeconn();
    
                }
            }   
    
  2. 现在创建行更新方法..

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string ParameterValue1 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
    
            string ParameterValue2 = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text; //Name
    
            UpdateOrAddNewRecord(ParameterValue1, ParameterValue2); // call update method
    
            GridView1.EditIndex = -1;
            BindGridView();
            Label2.Visible = true;
            Label2.Text = "Row Updated";
        }
    
  3. 创建行取消事件..

    protected void GridView1_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)     {         GridView1.EditIndex = -1; // swicth回到默认模式         BindGridView();     }

  4. 创建行编辑...

    protected void GridView1_RowEditing(object sender,GridViewEditEventArgs e)     {         GridView1.EditIndex = e.NewEditIndex;         BindGridView();     }

  5. 还有很多其他出路以不同的方式进行同样的活动。这是最基本的方式。无论如何,如果你觉得它有用,请把它标记为你的答案,否则让我知道......

答案 3 :(得分:0)

In your code I think you have not handled the event for "Update".


Have a look at the below example hope it might help you,
check for the "UpdateCommand".
Also write a Update event in C# to update.



<asp:DetailsView ID="ManageProducts" runat="server" AllowPaging="True"
    AutoGenerateRows="False" DataKeyNames="ProductID"
    DataSourceID="ManageProductsDataSource" EnableViewState="False">
    <Fields>
        <asp:BoundField DataField="ProductID" HeaderText="ProductID"
            InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
        <asp:BoundField DataField="ProductName" HeaderText="ProductName"
            SortExpression="ProductName" />
        <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
            SortExpression="UnitPrice" />
        <asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
            SortExpression="Discontinued" />
    </Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="ManageProductsDataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
    DeleteCommand=
        "DELETE FROM [Products] WHERE [ProductID] = @ProductID"
    InsertCommand=
        "INSERT INTO [Products] ([ProductName], [UnitPrice], [Discontinued])
         VALUES (@ProductName, @UnitPrice, @Discontinued)"
    SelectCommand=
        "SELECT [ProductID], [ProductName], [UnitPrice], [Discontinued]
         FROM [Products]"
    UpdateCommand=
        "UPDATE [Products] SET [ProductName] = @ProductName,
         [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued
         WHERE [ProductID] = @ProductID">
    <DeleteParameters>
        <asp:Parameter Name="ProductID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="ProductName" Type="String" />
        <asp:Parameter Name="UnitPrice" Type="Decimal" />
        <asp:Parameter Name="Discontinued" Type="Boolean" />
        <asp:Parameter Name="ProductID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="ProductName" Type="String" />
        <asp:Parameter Name="UnitPrice" Type="Decimal" />
        <asp:Parameter Name="Discontinued" Type="Boolean" />
    </InsertParameters>
</asp:SqlDataSource>