简体   繁体   中英

ASP.NET 3.5 GridView Row Selection

I have a GridView . I have to collect the GridViewRow where checkbox is selected. How can I achieve it without any client side script? Please help me to get this done.

If you are familiar with LINQ,you can get this something like

List<GridViewRow> rowCollection = 
                     GridView1.Rows
                     .OfType<GridViewRow>()
                     .Where(x => ((CheckBox)x.FindControl("chkRow")).Checked)
                     .Select(x => x).ToList();

All the best.

另一种老式方法是使用for或foreach循环遍历网格的Rows集合,使用FindControl方法找到复选框并检查其Checked属性值。

Simple and easy to understand when you come back to it later.

 var selectedRows = (from GridViewRow row in GridView1.Rows
                    let cbx = (CheckBox)row.FindControl("CheckBox1")
                    where cbx.Checked
                    select row).ToList();

Bare in mind that for this to work I think you'll need to convert the column containing the checkbox into a template column.

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