How to use LINQ to get selected items (checkboxes) within a GridView control that allow paging?

Posted on 2009-05-21
Medium Priority
Last Modified: 2013-12-17
I'm exploring the possibility to use LINQ to get selected items (checkboxes) within a GridView control that allow paging.

Checkbox is added like this:

                      <asp:CheckBox ID="chk" runat="server" />

Anyone can advise here? Thanks
Question by:Ryan Chong
  • 3
LVL 39

Accepted Solution

abel earned 1996 total points
ID: 24454777
It seems that you want to query all checkboxes within a GridView, from outside the GridView (i.e., not from within any updating event of the checkbox_change event).

Reading your request, I though it wasn't trivial, but it isn't that hard, really. This will give you what you want:

var checkedCheckBoxes = from GridViewRow row in GridView1.Rows 
                        where (row.FindControl("chk") as CheckBox).Checked
                        select row.FindControl("chk");
foreach(CheckBox chk in checkedCheckBoxes)
    Debug.WriteLine("Text is: " + chk.Text);

Open in new window

LVL 39

Expert Comment

ID: 24454787
(PS: there's also a LINQ zone, in case you want to make any follow-up questions....;-).
LVL 56

Author Comment

by:Ryan Chong
ID: 24469933
Hi there,

Thanks for your codes, have some fun with it, and my final outcome is something like below:

var checkedIDs = from GridViewRow row in GridView1.Rows
                            where (row.FindControl("chk") as CheckBox).Checked
                            select (row.FindControl("id") as HiddenField).Value;


LVL 39

Expert Comment

ID: 24471254
Looks good. Tx for the grade! ;-)

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question