1

如何将 GridView 中行的 id 链接到 deletecommand 以删除一行?

这是我的代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AllowSorting="True" DataSourceID="SqlDataSource1" 
        AutoGenerateColumns="False">
    <Columns>
        <asp:CommandField ShowDeleteButton="True" />
        <asp:BoundField DataField="SDate" HeaderText="SDate" SortExpression="SDate" />
        <asp:BoundField DataField="STime" HeaderText="STime" SortExpression="STime" />
        <asp:BoundField DataField="SPrice" HeaderText="SPrice" 
                SortExpression="SPrice" />
        <asp:BoundField DataField="SDuration" HeaderText="SDuration" 
                SortExpression="SDuration" />
        <asp:BoundField DataField="SNoPlaces" HeaderText="SNoPlaces" 
                SortExpression="SNoPlaces" />
        <asp:BoundField DataField="ScheduleId" HeaderText="ScheduleId" 
                InsertVisible="False" ReadOnly="True" SortExpression="ScheduleId" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>" 
        DeleteCommand="DELETE FROM Schedule WHERE (ScheduleId = @ScheduleId)" 
        SelectCommand="SELECT SDate, STime, SPrice, SDuration, SNoPlaces, ScheduleId FROM Schedule">
    <DeleteParameters>
        <asp:Parameter Name="ScheduleId" />
    </DeleteParameters>
</asp:SqlDataSource>

我正在使用 Visual Studio 2010。

4

2 回答 2

3

您忘记在 GridView 控件上设置DataKeyNames 属性。

改变你的 GridView 像:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False"
    DataKeyNames="ScheduleId" >

再举一个例子:SqlDataSource.DeleteCommand 属性

于 2012-08-31T13:28:53.723 回答
0

id在 Gridview 属性中链接您的

DataKeyNames="your_id"

您还可以定义多个ids,如下所示:

DataKeyNames="your_id1, your_id2"

您可以访问以下值:

protected void GridView1_Deleting(object sender, ObjectDataSourceMethodEventArgs)  
{
    //e.Command.Parameters["your_id1"].Value;
}
于 2012-08-31T13:30:14.557 回答