Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid Column Hiding/Resizing

ASP.NET Forum General Use

Datagrid Column Hiding/Resizing

by  henslecd  Posted    (Edited  )
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
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top