gridview和 sqldatasource都是自动生成的,现在我在gridview上新加了一列checkbox,并在个gridview外面加了一个删除按钮,我希望先checkbox选责一些行,然后点删除按钮,把这些行批量删除.主要是我不想用gridview自带的删除,怎么办??

解决方案 »

  1.   

    foreach(GridViewRow row in GridView1.Rows) {
         CheckBox chk = row.FindControl("MyCheckBoxID") as CheckBox;
         if(chk != null && chk.Checked) { // 此行被选中,
               object key = GridView1.DataKeys[row.RowIndex].Value; // 获取键
               // 以下执行删除 ...
         }
    }
      

  2.   

    Jinglecat(晓风残月 >> 问题需简洁,错误要详细,需求得明确)我的代码已经到了这个程度了,主要就是以下执行删除怎么办??我的sqldatasource是自动生成的
    后台
        protected void Button3_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow dvr in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)dvr.Cells[4].FindControl("CheckBox1");
                if (cb.Checked == true)
                {
                    
                    ??这里怎么写
                }
            }
        }
    前台
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
                DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnRowCreated="GridView1_RowCreated" OnRowDataBound="GridView1_RowDataBound" PageSize="5"  >
                <Columns>
                    <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                        ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                    <asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="UnitPrice"
                        SortExpression="UnitPrice" />
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:CheckBox ID="CheckBox2" runat="server" Text="全选" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
                ConnectionString="Data Source=DLZ-18A1E0ACAFD;Initial Catalog=Northwind;User ID=sa"
                DeleteCommand="DELETE FROM Products WHERE (ProductID = @original_ProductID)"
                InsertCommand="INSERT INTO [Products] ([ProductName], [UnitPrice]) VALUES (@ProductName, @UnitPrice)"
                OldValuesParameterFormatString="original_{0}" ProviderName="System.Data.SqlClient"
                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]"
                UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [UnitPrice] = @UnitPrice WHERE [ProductID] = @original_ProductID AND [ProductName] = @original_ProductName AND [UnitPrice] = @original_UnitPrice">
                <DeleteParameters>
                    <asp:Parameter Name="original_ProductID" Type="Int32" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="ProductName" Type="String" />
                    <asp:Parameter Name="UnitPrice" Type="Decimal" />
                    <asp:Parameter Name="original_ProductID" Type="Int32" />
                    <asp:Parameter Name="original_ProductName" Type="String" />
                    <asp:Parameter Name="original_UnitPrice" Type="Decimal" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="ProductName" Type="String" />
                    <asp:Parameter Name="UnitPrice" Type="Decimal" />
                </InsertParameters>
            </asp:SqlDataSource>
            <br />