INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
Are you a Computer / IT professional? Join Tek-Tips now!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Feedback
"...I love this site! It's so nice to know that there are so many people out there who are willing to share their knowledge..."
Geography
Where in the world do Tek-Tips members come from?
|
ASP.NET Forum General Use
|
Datagrid Column Hiding/Resizing
Posted: 16 Apr 04 (Edited 16 Apr 04)
|
Here is some code that would be useful for reports where your user would like to choose the columns of the datagrid that he or she wants to see.
I use a checkboxlist with a checkbox for each column of my datagrid. The checkboxes are created on the fly in the pageload event. There is also a button at the end that triggers the event.
CheckBoxList
'******************************************
Select data you would like to view <asp:checkboxlist id="check1" runat="server" RepeatDirection="Horizontal" </asp:checkboxlist>
<asp:button id="Button3" runat="server" Width="108px" Text="Select Data"></asp:button>
'******************************************
The next part is the datagrid.
Note: I didn't include all the datagrid attributes for clarity.
Datagrid
'******************************************
<asp:datagrid id=DataGrid1 runat="server" Width="500px" Height="232px"> <Columns> <asp:BoundColumn DataField="VENDOR" HeaderText="VENDOR"></asp:BoundColumn> <asp:BoundColumn DataField="PHONE" HeaderText="PHONE"></asp:BoundColumn> <asp:BoundColumn DataField="EMAIL" HeaderText="EMAIL"></asp:BoundColumn> <asp:BoundColumn DataField="ADDRESS" HeaderText="ADDRESS"></asp:BoundColumn> <asp:BoundColumn DataField="WEBSITE" HeaderText="WEBSITE"></asp:BoundColumn> </Columns>
'******************************************
In the pageload event I add the appropriate checkboxes.
'******************************************
Dim i As Integer
If Not IsPostBack Then
For i = 0 To DataGrid1.Columns.Count - 1
If Not DataGrid1.Columns(i).HeaderText = "" Then
check1.Items.Add(New ListItem(DataGrid1.Columns(i).HeaderText))
End If Next
End If
'******************************************
Finally I have the button's click event behind the page. The commented out lines of code will let you set specific column size. If you do not uncomment these lines of code, the columns will shrink to whatever the longest value's length in the column is. Setting the column width makes reports look a lot cleaner, but if you are pressed for space then just leave those lines commented.
'******************************************
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim i As Integer
'Dim columnwidth As Integer = 100 Dim maxcolumns As Integer = check1.Items.Count
For i = 0 To maxcolumns - 1
DataGrid1.Columns(i).ItemStyle.Wrap = False
'DataGrid1.Columns(i).HeaderStyle.Width = Unit.Pixel(columnwidth)
If Not check1.Items(i).Selected Then
DataGrid1.Columns(i).Visible = False
Else
DataGrid1.Columns(i).Visible = True
End If
Next
End Sub
'******************************************
I welcome any suggestions to improving it. Let me know if you have questions.
Chad |
Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum |
|
 |
|
Join Tek-Tips® Today!
Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.
Here's Why Members Love Tek-Tips Forums:
Talk To Other Members
- Notification Of Responses To Questions
- Favorite Forums One Click Access
- Keyword Search Of All Posts, And More...
Register now while it's still free!
Already a member? Close this window and log in.
Join Us Close