473,382 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Hiding and Showing Columns in the DataGrid Control...

The DataGrid allows you to make columns visible or invisible on demand
- even edit and other special columns. This article will show you how
it is done.

Some developers have reported problems controlling the visibility of
columns in the DataGrid control. The problem usually comes down to one
fact. The DataGrid has a property called AutoGenerateColumns. The
default value is "True". This means that when AutoGenerateColumns is
set to True, the DataGrid will pull its column headings straight from
the column names of the database table. If you allow this to happen
you cannot control the visibility of the columns. You must use
asp:BoundColumns and specify the HeaderText and Datafield properties
in your DataGrid setup. This relatively simple change allows you
accomplish the hiding and showing of columns as you wish. It also
works with Edit and other special columns as you will see.

First, lets look at the html code in our .aspx page. Immediately below
the
tag we have two buttons and set OnClick event handlers to two
subroutines which will do the work of hiding and showing the
appropriate columns in our code-behind file. In the DataGrid code,
notice that AutoGenerateColumns is set to "False". Also notice that we
have used asp:BoundColumns to set our HeaderText and DataFields. I
have also set the Visible property to false so that nothing but the
CompanyName column will be shown at startup. I have also added an Edit
column although I did not wire it up to actually do any editing (the
copy of the Customers table in the Northwind database I am using is
readonly anyway). After the bound columns are some other datagrid
properties just to make the grid look good.







AutoGenerateColumns="False"
BorderColor="#999999"
BorderStyle="None"
BorderWidth="1px"
BackColor="White"
CellPadding="3"
GridLines="Vertical">







Now for the code-behind file which is shown in two parts. The first
part displayed below is just the usual page_load event which calls a
"BindTheData" routine to actually get the data from the Customers
table. All of this should be self explanatory.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Class ShowHideCols : Inherits System.Web.UI.Page
Protected btnShow As System.Web.UI.WebControls.Button
Protected btnHide As System.Web.UI.WebControls.Button
Protected dtgCusts As System.Web.UI.WebControls.DataGrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindTheData
End If
End Sub

Sub BindTheData
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
objConn = New SqlConnection(ConfigurationSettings.AppSettings("N orthwindConnection"))
Dim strSql As String
strSql = "SELECT Top 10 CompanyName, ContactName, ContactTitle,
City, Country, Phone FROM Customers"
objCmd = New SqlCommand(strSql, objConn)
objConn.Open
dtgCusts.DataSource = objCmd.ExecuteReader()
dtgCusts.DataBind()
objConn.Close()
objConn.Dispose()
End Sub
Lastly comes our two subroutines to handle the showing and hiding of
the columns in the grid other than Company Name. Since Company Name is
Column(0), these routines use a simple For...Next loop to cycle
through the remaining columns setting the visible property to true or
false as appropriate.

Sub ShowDetails(sender As System.Object, e As System.EventArgs)
Dim intCounter As Integer
For intCounter = 1 to dtgCusts.Columns.Count - 1
dtgCusts.Columns(intCounter).Visible = True
Next
End Sub

Sub HideDetails(sender As System.Object, e As System.EventArgs)
Dim intCounter As Integer
For intCounter = 1 to dtgCusts.Columns.Count - 1
dtgCusts.Columns(intCounter).Visible = False
Next
End Sub

End Class
There you have it. A simple way to control the visible property of
your DataGrid columns.
AMBER [MCSD.NET MCAD.NET] http://www.dedicatedsolutions.co.uk
Nov 17 '05 #1
1 3337
>If you allow this to happen
you cannot control the visibility of the columns. You must use
asp:BoundColumns and specify the HeaderText and Datafield properties
in your DataGrid setup.


This is not correct at all. Hiding autogenerated columns is easily
accomplished in the itemdatabound/created event handler by setting the
Cell's visible property to false.

Microsoft discourages developers from using the autogenerated feature except
for prototyping. I believe this to be a flawed recommendation on their part,
coupled to the fact that it is on by default. Developers should not fear the
autogenerate feature of a datagrid because it is very powerful, contains
most if not all the power of bound columns and results in a faster more
efficient datagrid render than if bound columns were used. The false premise
that microsoft builds on is that you do not have full control of the
datagrid when autogenerated columns is used. This is absolutely false.
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: c.anandkumar | last post by:
Hi All - I have some problems getting a small piece of javascript working correctly for Firefox. Here is what I am trying to do - 1. I have a form (like a search form) 2. I have many groups...
2
by: Kevin | last post by:
Hi Al Can someone tell me how to hide colums in a datagrid. I have a one datagrid that is a master and child and I would like to hide specific columns, how do I do this TI Kevin
1
by: Udi | last post by:
Hi everyone! I have a major problem with the DataGrid control for the Windows Form! I am using the DataAdapter to get data from Sql Server 2000 DataBase and populating a DataGrid with the Table i...
2
by: MrNobody | last post by:
I found some tutorial which had the following example for hiding columns in a DataGrid: ...
2
by: Peter Rilling | last post by:
Okay, I know that the DataKeyField property of a DataGrid is supposed to allow me to define a field which will not be displayed. The values in that column end up in the DataKeys collection. My...
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
0
by: klynn | last post by:
I queried a series of fields from a database and passed into a datatable. Then bound this to a datagrid. I need to hide 2 of the colums. This doesnt seem to be working the standard way. (with...
9
by: tshad | last post by:
I have a datagrid that I want to add a new column to. This column will only be visible under certain conditions. So I want to set the column visible=false. Then when the right condition happens...
0
by: Bob Davies | last post by:
Hi I have a webservice that retrieves data from a database, this is then returned to the calling client application built in windows forms within a dataset, however upon attempting to create...
5
by: Rich | last post by:
I have a datagrid that uses a dataview object for its datasource. The dataview contains a unique row identifier column. I don't want to display this column in the datagrid. I need the unique row...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.