How do I get a column of checkboxes in a Gridview?

One note: I am doing all of this in Visual Studio 2013 Express for the web.

I have a GridView and its data is generated by SqlDataSource. Currently when I am testing the page. A table similar to the one below is created:

CustomerID    CustomerName    CustomerAddress
-----------   ------------    ----------------
1             Bob             Address
2             John            Address
3             Smith           Address

      

However, I really want this:

CustomerID    CustomerName    CustomerAddress
-----------   ------------    ----------------
[]             Bob             Address
[]             John            Address
[]             Smith           Address

      

I want the CustomerID field to be a "hidden field" and have a checkbox on it. Then I want the checkboxes value to be the CustomerID for that row. However, I can't for the rest of my life get the checkboxes there and it still just displays the CustomerID. Eventually, I want to create a button that will delete the rows of what was checked and reflect it in the database via DELETE FROM TABLE

. This is why I want the checkboxes to also "have" the value of any CustomerID for that particular row.

Here's the code:

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID" DataSourceID="RowsInGroup" >
            <Columns>
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
                <asp:BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName" />
                <asp:BoundField DataField="CustomerAddress" HeaderText="CustomerAddress" SortExpression="CustomerAddress" />
                <asp:TemplateField></asp:TemplateField>
            </Columns>
</asp:GridView>

      

If there is a better Data object in the toolbox, I'm all ears.

Thanks for any advice you can provide!

+3


source to share


1 answer


You have already solved half of the problem. By using DataKeyNames="CustomerID"

, you don't need the hidden field to hold that value.

First, create a checkbox column. There are several ways to do this. Here is one of them:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkDelete" runat="server" />
    </ItemTemplate>
</asp:TemplateField>

      



Then delete is handled anyway, just repeat every row in the GridView and check the box on every row. If it is installed, use DataKey

for that line to get CustomerID

.

protected void btnDelete_Click(object sender, EventArgs e)
{
    List<string> customersToDelete = new List<string>();
    foreach(GridViewRow row in GridView1.Rows)
    {
        CheckBox chkDelete = (CheckBox)row.FindControl("chkDelete");
        if(chkDelete.Checked)
        {
            DataKey key = GridView1.DataKeys[row.DataItemIndex];
            customersToDelete.Add(key.Value.ToString());
        }
    }
}

      

+4


source







All Articles