Print Page | Close Window

Checkbox in Datagrid Column

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=13311
Printed Date: 29 March 2026 at 7:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Checkbox in Datagrid Column
Posted By: sabrin514
Subject: Checkbox in Datagrid Column
Date Posted: 11 January 2005 at 10:31am
I have a datagrid that lists results of a user-run search. I would like to add checkboxes in each column where the user can check/uncheck the checkbox in each column to hide columns he/she doesn't wish to see.
 
The problem is that I am having trouble accessing checkboxes in the columns as column does not appear to contain "item".  I have read endless tutorials and discussion forums about checkboxes in datagrids, but it seems that everyone else is using them to highlight rows in the grid (a la Hotmail).
 
I have also had problems referencing my checkboxes.... "not set to an instance of an object". I think I have resolved that issue for the most part, but the problem with putting checkboxes in the columns and then referencing them from there still remains.
 
Is there something I am missing? I can't believe that I am the only person to want to allow users to dynamically hide/unhide columns in a datagrid list. 
 
Any advice or help would be greatly appreciated. Thank you. 
 
 
 



Replies:
Posted By: sabrin514
Date Posted: 11 January 2005 at 10:33am
Oh I forgot to mention that once columns are hidden, the user can again display them all by clicking a "show all" button that would set the visible column of each column to true again.


Posted By: michael
Date Posted: 11 January 2005 at 11:08am
That is just some pseudo code I have not tested, but should give you the idea:

<asp:datagrid id="DataGrid1" runat="server">
the in each column put something like
<FooterTemplate>
    <asp:Checkbox id="chkHide[N]" runat="server" CommandName="Select"></asp:TextBox>
</FooterTemplate>
replace N for each column with a number
 
Then in your Codebehind:
 
Private Sub DataGrid1_ItemCommand(.......) Handles  DataGrid1.ItemCommand
 If e.CommandName = "Select" Then
    Dim c1 As CheckBox = e.Item.FindControl("chkHideCol1")
    'Repeat the above for each column
   
    If c1.Checked THEN
        DataGrid1.Columns(1).Visible = False        
    End IF
 End IF


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: michael
Date Posted: 11 January 2005 at 11:09am
</asp:TextBox> is checkbox of course, sorry


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: sabrin514
Date Posted: 11 January 2005 at 11:22am
Thanks. I'll give it a try. Would it make any difference if I substituted  HTML checkboxes (using runat="server") for the asp checkboxes? Thanks again.


Posted By: Mart
Date Posted: 11 January 2005 at 11:23am
Yes, a html checkbox will be of the type HtmlGenericControl and an asp one if type of Checkbox


Posted By: sabrin514
Date Posted: 11 January 2005 at 2:58pm
Thanks for your response. I am still having some problems though.
 
I would prefer to not use the footer as the datagrid result lists can be quite large and I do not want the user to have to reach the end to hide/unhide columns. I stuck the asp checkbox in the header instead and it seems to be okay, except...
 
CommandName is apparently not part of the asp checkbox. Do I need to add attributes? If so, how do I do that? I realize that any added attributes probably would need to be taken care of on or before the page load event.
 
Also, the asp checkbox does have a built in oncheckedchanged event, however, I have having trouble utilizing this because I am having trouble referencing the checkboxes using findcontrol(checkbox name) because I cannot pass in the item I need to or it is not part of the params I can pass in with the oncheckedChanged event.
 
If you have any advice, I'd be grateful. I feel like I am banging my head against a wall with this. The final solution will probably end up being far more simple than I am imaging it to be at this point.
 
 


Posted By: sabrin514
Date Posted: 11 January 2005 at 3:16pm
Oh I forgot to mention this and I'm not sure it makes any difference, but I also have asp linkbuttons in the column headers so that the user can sort the columns in asc or desc order. That seems to work fine since link buttons have a commandName and commandargument attribute built in.  Checkboxes don't, and there does not seem to be any Item property or attribute for column headers.... only the "items" in the grid; therefore I am having a heck of a time referencing them. Sorry if I am misusing the terminology.


Posted By: michael
Date Posted: 11 January 2005 at 4:27pm
why do you have to use a checkbox anyway? there is no way to uncheck them as they are hidden so a checkbox is kinda pointless. I'd just use an imagebutton with some graphic indicating that it'll be hidden.


-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: sabrin514
Date Posted: 13 January 2005 at 11:06am
Well, I was going to have a "show all" button that would unhuide all the columns...


Posted By: michael
Date Posted: 13 January 2005 at 2:47pm
right, but that button would be outside of the datagrid. I mean, I don't want to tell you how to do it of course, but a checkbox is a 2 value control, thus you would only be able to use one value, that is why IMHO a small hide image would be better and it sure has the itemcommand property.

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net