I have to display some tabular data where there is a maximum of 3 columns to be displayed, but sometimes, only 1 or 2 are required. Thus far, I have:
<asp:FormView ID="myFormView" runat="server" DataSourceID="myXmlDataSource">
<ItemTemplate>
<table cellspacing="0" cellpadding="0" class="myTableStyle">
<colgroup>
<col class="myCol1Style" />
<col class="myCol2Style" />
<col class="myCol3Style" />
</colgroup>
<thead>
<tr>
<th class="myCol1HeaderStyle">Column 1</th>
<th class="myCol2HeaderStyle">Column 2</th>
<th class="myCol3HeaderStyle">Column 3</th>
</tr>
</thead>
<tr>
<td>
<p>Column 1 data goes here</p>
</td>
<td>
<p>Column 2 data goes here</p>
</td>
<td>
<p>Column 3 data goes here</p>
</td>
</tr>
</table>
</ItemTemplate>
</asp:FormView>
<asp:XmlDataSource ID="myXmlDataSource" runat="server" />
Column 1 will always be displyed, but in some cases, I’ll need to hide column 2 and/or 3.
What would be the best way to handle this?
I would suggest using a GridView control with “AutoGenerateColumns” set to true, instead of a FormView control. Then bind it to your datasource, and it will always show the same number of columns that your datasource has.
If you need to hide columns on the basis of user events rather than the data in them, then GridView ultimately will give you a lot more control over rows/columns.