简体   繁体   English

从GridView删除行

[英]deleting row from gridview

I have 2 tables MachineGroups and Machines. 我有2个表MachineGroups和Machines。 MachineGroups has columns: MachineGroups具有以下列:

MachinegroupID
MachineGroupName
MachineGroupDesc

And Machines has columns: 机器具有以下列:

MachineGroupID (FK)
MachineID
MachineName
Machinedesc

Now I want to delete a machinegroup but not the ones that have machines in it. 现在,我要删除一个计算机组,而不要删除其中有计算机的计算机组。

DELETE FROM MachineGroups
 WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)

This will delete all the rows in the gridview which have no. 这将删除gridview中所有没有的行。 of machines= 0 What i want is to delete only the row whose delete link is clicked... 的计算机= 0我想要的是仅删除单击了删除链接的行...

<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
    AutoGenerateColumns="False" CellPadding="1" CellSpacing="2"
    DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None"
     Width="100%" ondatabound="GridView1_DataBound1"
    onrowdatabound="GridView1_RowDataBound1">
    <RowStyle BackColor="#D0D8E8" ForeColor="#333333" Height="35px" />
    <Columns>
        <asp:BoundField DataField="MachineGroupID" HeaderText="MachineGroupID" 
            InsertVisible="False" ReadOnly="True" SortExpression="MachineGroupID" 
            Visible="False" />
        <asp:BoundField DataField="MachineGroupName" HeaderText="MachineGroupName" 
            SortExpression="MachineGroupName" />
        <asp:BoundField DataField="MachineGroupDesc" HeaderText="MachineGroupDesc" 
            SortExpression="MachineGroupDesc" />
        <asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded" 
            SortExpression="TimeAdded" />
        <asp:TemplateField HeaderText="CanBeDeleted" SortExpression="CanBeDeleted" 
            Visible="False">
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"
                    Checked='<%# Bind("CanBeDeleted") %>' />
            </EditItemTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server"
                    Checked='<%# Bind("CanBeDeleted") %>' Enabled="false" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="No. of PCs" HeaderText="No. of PCs" ReadOnly="True"
            SortExpression="No. of PCs" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
                    CommandName="Delete" Text="Delete"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>


<asp:SqlDataSource ID="SqlDataSource1" runat="server"         ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>" 
    SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted" 
    DeleteCommand="DELETE FROM MachineGroups
 WHERE MachineGroupID NOT IN (SELECT DISTINCT MachineGroupID FROM Machines)">
    <DeleteParameters>
        <asp:Parameter Name="original_MachineGroupID" />
        <asp:Parameter Name="original_MachineGroupName" />
        <asp:Parameter Name="original_MachineGroupDesc" />
        <asp:Parameter Name="original_CanBeDeleted" />
        <asp:Parameter Name="original_TimeAdded" />
    </DeleteParameters>
</asp:SqlDataSource>

Not tested, but the direction should be right: 未经测试,但方向应该正确:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>" 
    SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS 'No. of PCs' FROM MachineGroups FULL OUTER JOIN Machines ON Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, MachineGroups.CanBeDeleted" 
    DeleteCommand="DELETE FROM MachineGroups WHERE MachineGroupID = @MachineGroupID">
    <DeleteParameters>
        <asp:Parameter Name="MachineGroupID" />
    </DeleteParameters>
</asp:SqlDataSource>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM