Busch_League
Busch_League

Reputation: 39

Delete checked rows from GridView

I have a gridview that I have added an ItemTemplate to display a checkbox for each row. I am trying to simply delete the rows checked in the code behind file. My issue is that my code behind does not detect the checkbox. It is very strange because I feel like I haven't changed anything, but the code no longer works (it worked as of two days ago, but locally saved these changes - did not upload to TFS).

GridView (EDITED TO INCLUDE BINDING):

  <%--Data Grid--%>
<asp:GridView ID="Grid_Recipe" CssClass="gridMain" runat="server" OnSelectedIndexChanged="Grid_Recipe_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="Recipe_ID" DataSourceID="DataSource_Grid_Unfiltered" EnableViewState="True" AllowPaging="True" AllowSorting="True" BorderStyle="Solid" BorderWidth="1px" CellPadding="1" CellSpacing="1" HorizontalAlign="Center">

    <AlternatingRowStyle BackColor="#CCFFFF" />

    <Columns>
        <asp:CommandField ShowSelectButton="True" SelectText="Edit" />
        <asp:TemplateField HeaderText="Select"> 
            <ItemTemplate> 
                <asp:CheckBox ID="deleteCheckbox" runat="server" /> 
            </ItemTemplate> 
        </asp:TemplateField> 
        <asp:BoundField DataField="Recipe_ID" HeaderText="Recipe_ID" InsertVisible="False" ReadOnly="True" SortExpression="Recipe_ID" Visible="false" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:BoundField DataField="Difficulty" HeaderText="Difficulty" SortExpression="Difficulty" />
        <asp:BoundField DataField="Meal" HeaderText="Meal" SortExpression="Meal" />
        <asp:BoundField DataField="Cook_Time" HeaderText="Cook Time" SortExpression="Cook_Time" />
        <asp:BoundField DataField="Directions" HeaderText="Directions" SortExpression="Directions" />
    </Columns>
    <HeaderStyle BackColor="#0096D6" ForeColor="White" HorizontalAlign="Right" />
</asp:GridView>

<%--Delete Button--%>
<asp:Button runat="server" ID="deleteButton" Text="Delete Checked" OnClick="DeleteRows" />


<%--Data Sources--%>
<asp:SqlDataSource ID="Meal_Filter" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT DISTINCT [Meal] FROM [Recipe]"></asp:SqlDataSource>
<asp:SqlDataSource ID="DataSource_Grid_Unfiltered" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT * FROM [Recipe]" UpdateCommand="UPDATE [Recipe] SET [Name] = @Name, [Difficulty] = @Difficulty, [Meal] = @Meal, [Cook_Time] = @Cook_Time, [Directions] = @Directions WHERE [Recipe_ID] = @Recipe_ID">
    <DeleteParameters>
        <asp:Parameter Name="Recipe_ID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Difficulty" Type="String" />
        <asp:Parameter Name="Meal" Type="String" />
        <asp:Parameter Name="Cook_Time" Type="Int32" />
        <asp:Parameter Name="Directions" Type="String" />
        <asp:Parameter Name="Recipe_ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSource_Grid_Meal" runat="server" ConnectionString="<%$ ConnectionStrings:LYNNAU_ConnectionString %>" SelectCommand="SELECT * FROM [Recipe] WHERE ([Meal] = @Meal)" UpdateCommand="UPDATE [Recipe] SET [Name] = @Name, [Difficulty] = @Difficulty, [Meal] = @Meal, [Cook_Time] = @Cook_Time, [Directions] = @Directions WHERE [Recipe_ID] = @Recipe_ID">
    <DeleteParameters>
        <asp:Parameter Name="Recipe_ID" Type="Int32" />
    </DeleteParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="Meal_DDL" Name="Meal" PropertyName="SelectedValue" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="Difficulty" Type="String" />
        <asp:Parameter Name="Meal" Type="String" />
        <asp:Parameter Name="Cook_Time" Type="Int32" />
        <asp:Parameter Name="Directions" Type="String" />
        <asp:Parameter Name="Recipe_ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

Code Behind:

[WebMethod]
protected void DeleteRows(object sender, EventArgs e)
{
    dbCRUD delete = new dbCRUD();
    foreach(GridViewRow grd in Grid_Recipe.Rows)
    {
        if(grd.RowType == DataControlRowType.DataRow)
        {
            if((grd.FindControl("deleteCheckbox") as CheckBox).Checked)
            {
                string id = Grid_Recipe.DataKeys[grd.RowIndex].Value.ToString();
                //int intID = Convert.ToInt32(Grid_Recipe.SelectedDataKey.Value);
                int intID = int.Parse(id);
                if(delete.DeleteRecord(intID) == 1)
                {
                    resultsDelete.Text = "SQL Exception";
                    resultsDelete.Visible = true;
                    break;
                }
                else if(delete.DeleteRecord(intID) == 2)
                {
                    resultsDelete.Text = "Non SQL Exception";
                    resultsDelete.Visible = true;
                    break;
                }
                else
                {
                    resultsDelete.Text = "Record(s) deleted";
                    resultsDelete.Visible = true;
                }
            }
        }
    }
}

Anything in my code that would prevent the checkbox from being detected? I've put a breakpoint on the method and I get to where it checks validation for the checkbox, but after it iterates through every row, no rows are detected. Thanks in advance!

Upvotes: 1

Views: 1295

Answers (1)

Kelsey
Kelsey

Reputation: 47726

It looks to me like server side it's not going to know anything about your GridView because you have EnableViewState="False" set.

Set that to true and see if it helps. If not check how you are binding the GridView and make sure you are not rebinding it again to a null value in your Page_Load or something before the DeleteRows function runs. Post how you bind as that will help diagnose the issue.

I have also posted recently an answer to a similar question that you can find here: iterate through gridview rows on button click

Upvotes: 1

Related Questions