简体   繁体   English

如何在Gridview中获取一列复选框?

[英]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. 需要注意的是,我在Visual Studio 2013 Express for Web中都是这样做的。

I have a GridView, and its data is being generated by a SqlDataSource. 我有一个GridView,它的数据是由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. 我希望CustomerID字段为“隐藏字段”,并在其位置有一个复选框。 Then, I want the value of the checkboxes to be the CustomerID for that row. 然后,我希望复选框的值是该行的CustomerID。 However, I can't for the life of me get the checkboxes in there, and it's still just displaying the CustomerID itself. 但是,我不能在我的生活中获得复选框,它仍然只是显示CustomerID本身。 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 . 最后,我想创建一个按钮,删除所检查的任何行,并通过DELETE FROM TABLE将其反映在数据库中。 This is why I want the checkboxes to also "have" a value of whatever the CustomerID is for that particular row. 这就是为什么我希望复选框也“拥有”该特定行的CustomerID值。

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. 如果在工具箱中有一个更好的Data对象,我会全力以赴。

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. 通过使用DataKeyNames="CustomerID" ,您无需隐藏字段来保存此值。

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. 然后在处理删除的任何事件中,只需迭代GridView中的每一行并找到每行中的复选框。 If it is checked, use the DataKey for that row to get the CustomerID . 如果选中它,请使用该行的DataKey获取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());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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