3

I am trying to enable bulk deleting in my web app. The data I am displaying is in a GridView and I want to add a column which would contain one checkbox (or any alternative) for every row. The user could mark rows to delete and then delete all of them at once.

When I add a CheckBoxField, it must be bound to a column which I, naturally, don't have in the DataTable. I tried adding a new column to the DataTable programatically, but it is rendered as read-only whatever I do.

The data is retrieved from the DB into a DataTable, which I add to a DataSet and bind to the GridView as its data source.

Is there a way I could achieve this?

4
  • How do you want to delete ? in a button click ? If so why do you need a column you can just check the grid for checked columns right? Commented Apr 28, 2017 at 14:01
  • @Krishna Yes, on a click of a button all selected should be deleted. But I don't know how to select the rows. CheckBox column is data bound, so I can't use that. Renders as read only. Commented Apr 28, 2017 at 14:05
  • That's what I am saying, there's no need for it to be data bound Commented Apr 28, 2017 at 14:06
  • But how to add an editable checkbox to every row which is not data bound? Commented Apr 28, 2017 at 14:08

1 Answer 1

7

You can add a TemplateField to your GridView with the CheckBox. You can even add one in the header for a "select all" checkbox. You will still need a separate delete button

<asp:GridView ID="GridView1" runat="server">
    <Columns>

        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" CssClass="headerCheckBox" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" CssClass="rowCheckBox" />
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

<script type="text/javascript">
    $(document).ready(function () {
        $("#<%= GridView1.ClientID %> .headerCheckBox").change(function (e) {
            $("#<%= GridView1.ClientID %> .rowCheckBox input").each(function () {
                $(this).prop("checked", $(e.target).prop("checked"));
            });
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I've added a javascript function that checks/unchecks all the checkboxes if the header is clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.