Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: ASP.NET FAQ

ASP.NET Forum General Use

Datagrid Column Hiding/Resizing by henslecd
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.



Select data you would like to view
<asp:checkboxlist id="check1" runat="server" RepeatDirection="Horizontal"

<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.



<asp:datagrid id=DataGrid1 runat="server" Width="500px" Height="232px">
<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"
<asp:BoundColumn DataField="WEBSITE" HeaderText="WEBSITE"></asp:BoundColumn>


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

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


            DataGrid1.Columns(i).Visible = True

            End If


    End Sub


I welcome any suggestions to improving it.  Let me know if you have questions.


Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

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:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close