Multi Select Checkbox column in GridView

I have a template column that has a checkbox in the item template, the checked property is bound to a  database column called Hide, I want the users to be able to update the visbility of rows through this checkbox, i.e checked ones are hidden, unchecked ones are visible, how can I do something like that?

Thanks in advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi JessyEzzy

Set the autopost back property of the checkbox to ture
And in CheckedChanged write following code
 CheckBox chk = (CheckBox)sender;
        GridViewRow gr = chk.NamingContainer;
        GridView1.Rows[gr.DataItemIndex].Visible = false;

Good luck
JessyEzzyAuthor Commented:
Thanks spdude but I want it to be multiple selections so the user checks/unchecks couple of rows and update their visibility together, not one at a time, I think it would be irritating for the user to wait for each postack after each check/uncheck is made.

Jason ScolaroCommented:

Same sort of method, but on button click:

For Each gvr As GridViewRow in GridView1.Rows
  If CType(gvr.Row.FindControl("CheckBox1"), CheckBox).Checked Then
    gvr.Visible = False
  End If

-- Jason
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

JessyEzzyAuthor Commented:
Sorry spdude and Jason but it seems you misunderstood my question or may be it's my fault I wasn't clear enough.

I don't want to hide the gridview row I want to update the hide column value in the database.
The gridview is bound to a News Articles table, so the site administrator needs an option to show/hide articles from the frontend, so having a list of news titles and a checkbox column he can check/uncheck which article is visible and which is hidden.

I hope I'm clear this time.

Jason ScolaroCommented:
As I started typing a response, I realized I was doing it in a .NET 1.1 methodology.  Since you are using .NET 2.0, you might be able to benefit from a CheckBoxField instead.  As a matter of fact, this could possibilty be handled all automatically for you, depending on whether or not you are using the new SqlDataSource (or AccessDataSource, or xxxDataSource) controls.  Since you don't have to use a TemplateField column.  

In any case, here was my .NET 1.1 method (which would work in .NET 2.0):  

No problem, same sort of logic in your Button click event, but we'll change it a bit to access your Primary Key field:

For intRowIndex As Integer = 0 to GridView1.Rows.Count - 1
  Dim row As GridViewRow = GridView1.Rows(intRowIndex)
  Dim intNewsID As Integer = CInt(GridView1.DataKeys(intRowIndex).Value)
  Dim blnHide As Boolean = CType(row.FindControl("CheckBox1"), CheckBox).Checked
  ' make change in database using intNewsID and blnHide

Good luck!
-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JessyEzzyAuthor Commented:
Thanks, I have applied your solution but I was wondering if there could be a way to enhance this solution to make it update only the records whose Hide field has changed, instead of calling the update query for all rows, I thought of something like comparing old and new value for the checkbox, but can't figure out how.

JessyEzzyAuthor Commented:
While waiting for a response for my last posted comment I was able to think of a solution, so what I have done is as follows, in the column that has the Hide checkbox I added another hidden one and while looping in the button event that performs the update operation, I compare the checked property of the two checkboxes and updates only the rows whose two checkboxes checked property are not equal.

Hope that would be useful for anyone who wants to make something similar to mine.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.