我正在尝试使用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个简单行。 我错过了什么?
答案 0 :(得分:1)
您必须将DataKeyNames
添加到Gridview,知道数据绑定的ID是什么。
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataSourceID="dsGridView"
AutoGenerateDeleteButton="True"
DataKeyNames="CustomerID">