Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Migrating BoundColumn to BoundField Fix

3.00/5 (1 vote)
6 Sep 2007CPOL1 min read 1  
BoundColumn => BoundField problem solution when converting a DataTable to a DataView.

Introduction

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

Background

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:

C#
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:

ASP.NET
<%@ 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.

History

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

License

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