We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Multi Select Checkbox column in GridView

JessyEzzy
JessyEzzy asked
on
Medium Priority
1,118 Views
Last Modified: 2010-08-05
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.
Comment
Watch Question

Commented:
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
spdude

Author

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.

Thanks.
Top Expert 2006

Commented:
JessyEzzy,

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
Next

-- Jason

Author

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.

Thanks.
Top Expert 2006
Commented:
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
Next

Good luck!
-- Jason

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

Thanks.

Author

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.

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.