2

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Simple solution is to use GridView etc which can automatically generate columns from data source. If you cannot use that then if you can use jquery then I've a method that I use in my code.

Define different classes for first,second,third columns. define three protected variables or use an array of classes to hide. Register this array as javascript array using ClientScriptManager. Then on page load use jquery to hide the columns or tds with the classes in the array.

C#:

protected string SecondCol="true";
protected string ThirdCol = "true";

void BinData(){
    ////
    ////DataBinding Code
    ////
    if(!SecondColumnNeeded) SecondCol="false";
    if(!ThirdColumnNeeded) ThirdCol="false";
}

ASPX:

      <tr>
            <td>
                <p>Column 1 data goes here</p>
            </td>
            <td class="second">
                <p>Column 2 data goes here</p>
            </td>
            <td class="third">
                <p>Column 3 data goes here</p>
            </td>
        </tr>     

 <script type="text/javascript">
 var s="<%= SecondCol %>";
 var t="<%= ThirdCol %>";

 $(document).ready(function(){
    if(s==="true"){
        $("td.second").hide();//or remove
    }
    //similarly remove third column and column headers also
 });
 </script>

hope this helps you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.