Arnold
Arnold

Reputation: 23

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.

Upvotes: 2

Views: 996

Answers (3)

Jamie
Jamie

Reputation: 988

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.

Upvotes: 1

Dick Lampard
Dick Lampard

Reputation: 2266

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.

Upvotes: 1

Seshan
Seshan

Reputation: 91

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.

Upvotes: 4

Related Questions