简体   繁体   中英

How to get a column of checkboxes in Gridview?

One note is that I'm doing this all in Visual Studio 2013 Express for Web.

I have a GridView, and its data is being generated by a SqlDataSource. Currently, when I test the page. A table like below gets generated:

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 "hidden field" and have a checkbox in its place. Then, I want the value of the checkboxes to be the CustomerID for that row. However, I can't for the life of me get the checkboxes in there, and it's still just displaying the CustomerID itself. In the end, I want to create a button that will delete the rows of whatever is checked, and have it reflected in the database via a DELETE FROM TABLE . This is why I want the checkboxes to also "have" a value of whatever the CustomerID is 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's a better Data object to use in the Toolbox, I'm all ears.

Thanks for any advice you can provide!

You already have half of your problem solved. By using DataKeyNames="CustomerID" , you have no need for a hidden field to hold this value.

First, create your check box column. There are multiple ways to accomplish this. Here is one:

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

Then in whatever event handles the delete, just iterate each row in the GridView and find the checkbox in each row. If it is checked, use the DataKey for that row to get the 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());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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