GridView中的ObjectDataSource和DeleteMethod

时间:2012-07-06 18:45:25

标签: gridview objectdatasource

我正在尝试使用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个简单行。 我错过了什么?

1 个答案:

答案 0 :(得分:1)

您必须将DataKeyNames添加到Gridview,知道数据绑定的ID是什么。

<asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" 
        DataSourceID="dsGridView" 
        AutoGenerateDeleteButton="True" 
        DataKeyNames="CustomerID">