hide column of table with codebehind

asp.net, c#, html

Solution

Assuming your table is static, you could dynamically apply a CSS `class` to any columns you want hidden:

<table>
    <tr>
        <th>
            Visible
        </th>
        <th class="<%= HiddenClassName %>">
            Possibly hidden
        </th>
    </tr>
    <tr>
        <td>
            Visible
        </td>
        <td class="<%= HiddenClassName %>">
            Possibly hidden
        </td>
    </tr>
</table>

In the code file, your property could be:

public string HiddenClassName { get; private set; }

The `hidden` style itself:

<style type="text/css">
  .hidden 
  {
      visibility: hidden;
      display: none;
  }
</style>

Problem

I have a (html)table (sample layout below) that I'd like to hide the last two columns under certain conditions, using the C# codebehind. I'd prefer not to do this by shrinking the width of the columns to 0, though I'm not ruling it out. There aren't any CSS classes really supplied to most of the rows and none of the columns. I've tried using a colgroup to set the display to none as well as the visibility to hidden on the colgroup, but with no luck. ``` _____________ |__|__|__|__| |__|__|__|__| |__|__|__|__| ``` to ``` _______ |__|__| |__|__| |__|__| ``` Any thoughts?

Original source

Related problems