My Account
Home Products Support Company Contact Buy

Hide a Column in Table Control

Learn how to hide a column using three lines of code, without a leftover blank column.
- Jing Ding, Senior Systems Consultant for The Ohio State University Medical Center

August 1, 2008
Iron Speed Designer V5.X

Introduction

In Iron Speed Designer-generated applications, it is easy to dynamically display and hide field controls at runtime. However, there are times this may cause undesirable side-effects.

For example, if you hide all controls in a TableControl column (including the header), instead of a hidden column, the result is a blank column. The blank column looks awkward and can confuse users. This occurs because the table layout default in Iron Speed Designer is coded in static HTML. Static HTML is not manipulatable in server code. In this article, I will demonstrate how to hide a column in a TableControl by forcing the HTML elements to run at the server level.

Implementation

Step 1: Make column elements run at server

In Iron Speed Designer design mode, add "runat" and "id" attributes to the <th> and the <td> elements of the column, as shown in the figure below.

Step 2: Find and hide the elements at server

You only need to add three lines of code to:

1. find the column header and hide it
2. loop through table rows, and
3. find the cell and hide it

C#:

public class EmployeesTableControl : BaseEmployeesTableControl {
    public EmployeesTableControl() {
        PreRender += new EventHandler(EmployeesTableControl_PreRender);
    }
 
    void EmployeesTableControl_PreRender(object sender, EventArgs e) {
        FindControl("HireDateHeader").Visible = false;
        foreach (EmployeesTableControlRow row in GetRecordControls())
            row.FindControl("HireDateCell").Visible = false;
    }
}

Visual Basic .NET:

Public Class EmployeesTableControl
    Inherits BaseEmployeesTableControl
    Public Sub New()
        AddHandler PreRender, AddressOf EmployeesTableControl_PreRender
    End Sub
 
    Sub EmployeesTableControl_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        FindControl("HireDateHeader").Visible = False
        For Each row As EmployeesTableControlRow In GetRecordControls()
            row.FindControl("HireDateCell").Visible = False
        Next
    End Sub
End Class

Result

The column is hidden without a leftover blank column!

About the Author

Jing Ding has a PhD in Computer Engineering, Bioinformatics and Computational Biology, and an M.S. in Toxicology from Iowa State University. He received his B.S. in biophysics from Fundan University in Shanghai, China. He is a self-taught programmer who "played" with assembly, C and C++ in the 1990s. He took a break from programming from 1997 to 2000. When he picked it up again in 2001, he worked with Java. Jing began working with C# and .NET in 2006.


  Privacy Statement