?
Solved

Multi Select Checkbox column in GridView

Posted on 2006-03-26
7
Medium Priority
?
1,073 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.
0
Comment
Question by:JessyEzzy
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:spdude
ID: 16293590
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
0
 
LVL 3

Author Comment

by:JessyEzzy
ID: 16293615
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.
0
 
LVL 22

Expert Comment

by:Jason Scolaro
ID: 16294661
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Author Comment

by:JessyEzzy
ID: 16296255
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.
0
 
LVL 22

Accepted Solution

by:
Jason Scolaro earned 150 total points
ID: 16296743
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
0
 
LVL 3

Author Comment

by:JessyEzzy
ID: 16343104
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.
0
 
LVL 3

Author Comment

by:JessyEzzy
ID: 16382450
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.

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

612 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