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.
Alternative old-school method is to iterate through the Rows collection of the grid with for or foreach cycle, find the checkboxes with the FindControl method and check their Checked property value.
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.
精彩评论