1

我正在尝试使用 ObjectDataSource 为我的 GridView 指定 DeleteMethod,但无论我尝试什么,似乎都不会调用 DeleteMethod。

我的代码是:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="dsGridView" AutoGenerateDeleteButton="True">
        <Columns>
            <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" 
                SortExpression="CustomerID" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstNenter code hereame" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="Country" HeaderText="Country" 
                SortExpression="Country" />
        </Columns>
    </asp:GridView>

        <asp:ObjectDataSource ID="dsGridView" runat="server" 
        SelectMethod="GetCustomerByCountry" DeleteMethod="DeleteRow" TypeName="WebApplication1.App_Code.DAL">
        <SelectParameters>
            <asp:ControlParameter 
                ControlID="DropDownList1" 
                DefaultValue="NULL" 
                Name="Country" 
                PropertyName="SelectedValue" 
                Type="String" />
        </SelectParameters>
        <DeleteParameters>
            <asp:Parameter 
                Name="CustomerID" 
                Type="Int32" />
        </DeleteParameters>
    </asp:ObjectDataSource>

public static void DeleteRow(int CustomerID)
    {
        Database1Entities de = new Database1Entities();

        var LQ = (from s in de.Customers
                  where s.CustomerID == CustomerID
                  select s).FirstOrDefault();

        if (LQ != null)
        {
            de.Customers.DeleteObject(LQ);
            de.SaveChanges();
        }
    }

我有一个名为“Database1Entities”的数据库实体模型,它包含 4 个简单的行。我错过了什么?

4

1 回答 1

1

您必须将DataKeyNamesGridview 添加到知道您的数据绑定的 id 是什么。

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" 
        DataSourceID="dsGridView" 
        AutoGenerateDeleteButton="True" 
        DataKeyNames="CustomerID">
于 2012-07-06T20:27:33.357 回答