Click here to Skip to main content
12,819,019 members (25,927 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 6 Sep 2007

Migrating BoundColumn to BoundField Fix

, 6 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
BoundColumn => BoundField problem solution when converting a DataTable to a DataView.


When converting DataTables to GridView controls in large (intranet), object reference errors often occur when a BoundColumn is set to "Visible=false".


In applications that use the DataTable control, BoundColumns are sometimes set to invisible when a developer wants to hide data from users while using hidden data in client-side applications such as JavaScript.

The BoundField however, doesn't render any data on the client side when "visible" is set to false. When converting large applications, this might cause some problems. The normal fix would be to set the "visible" property programmatically to false after the "OnDataBind" event. However, this solution costs time.

To solve this problem in a short time span, I made my own class that uses BoundField as its base. The class simply solves the issue by setting the value of the field to visible on init, and restoring it after the field is bound.

Using the Code

To use this solution, place a new class in your app code called BoundColumnField. Add the following code to that class:

namespace GridViewFix 
    public class BoundColumnField : BoundField
        private Boolean NotVisible;
        protected override void InitializeDataCell(
                  DataControlFieldCell cell, DataControlRowState rowState)
            NotVisible = false;
            //If the Field isn't set to visible=true
            //the control will set it to visible.

            if (!this.Visible)
                this.Visible = true;
                NotVisible = true;
            base.InitializeDataCell(cell, rowState);

        protected override void OnDataBindField(object sender, EventArgs e)
            base.OnDataBindField(sender, e);
            //If the Field had visible=False the field will be hidden after databind. 
            if (NotVisible) this.Visible = false;

First register your namespace on the top of each page where you want to use this control, as follows:

<%@ Register TagPrefix="GridViewFix" Namespace="GridViewFix"%>

Next, you can use the class in your DataGrids (using <GridViewFix:BoundColumnField> instead). The usage of the class is the same as that of the BoundField class. Only the render behavior is compatible with the old BoundColumn control.

Points of Interest

Microsoft removed the rendering of invisible fields for security reasons. This fix changes the behavoir of hidden fields. Please keep in mind that hidden values will be accessible to users when they open the client-side code.


  • 1.0: Added my first article here - 06 September 2007.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Currently Junior Software Developer for Atos Origin. Working at BAS Netherland for the Microsoft Unit.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 6 Sep 2007
Article Copyright 2007 by JornP
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid