Thando Tee
Thando Tee

Reputation: 121

Hide ListView column in visual studio 2010 C#

Am using VS2010, I have an aspx page that has a ListView. Now I want to hide the ListView column that has the ID.

I have tired this code:

ListView1.Columns[0].Width = 0;

But it won't let me, it gives an error on the 'columns' part saying "system.web.ui.webcontrols.listview does not contain a definition for 'columns' and no extension method 'columns' accepting a first argument of type 'system.web.ui.webcontrols.listview' could not be found (are you missing a using directive or an assembly reference?)".

I also tried this code:

ListView1.FindControl("idTh").Visible = false;
         foreach (ListViewItem item in ListView1.Items)
        {
            item.FindControl("idCol").Visible = false;
        } 

But it gives me an "Object not set to reference" error.

What am I doing wrong??

This is my aspx code:

<asp:ListView ID="ListView1" runat="server" DataSourceID="Paysment_Dates" 
        InsertItemPosition="LastItem" 
        onselectedindexchanged="ListView1_SelectedIndexChanged">
        <AlternatingItemTemplate>
            <tr class="t_even">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td >
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                        Text="Update" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Cancel" />
                </td>
                <td>
                    <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>'  />
                </td>
                <td>
                    <asp:TextBox ID="PublishYearTextBox" runat="server" 
                        Text='<%# Bind("PublishYear") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishMonthTextBox" runat="server" 
                        Text='<%# Bind("PublishMonth") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PayDteTextBox" runat="server" Text='<%# Bind("PayDte") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishDteTextBox" runat="server" 
                        Text='<%# Bind("PublishDte") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="">
                <tr>
                    <td>
                        No data was returned.</td>
                </tr>
            </table>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                        Text="Insert" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                        Text="Clear" />
                </td>
                <td>
                    <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishYearTextBox" runat="server" 
                        Text='<%# Bind("PublishYear") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishMonthTextBox" runat="server" 
                        Text='<%# Bind("PublishMonth") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PayDteTextBox" runat="server" Text='<%# Bind("PayDte") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PublishDteTextBox" runat="server" 
                        Text='<%# Bind("PublishDte") %>' />
                </td>
            </tr>
        </InsertItemTemplate>
        <ItemTemplate>
            <tr class="t_odd">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td id="idCol" runat="server">
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">
                                </th>
                                <th id="idTh" runat="server">
                                    ID</th>
                                <th runat="server">
                                    Publish Year</th>
                                <th runat="server">
                                    Publish Month</th>
                                <th runat="server">
                                    Pay Date</th>
                                <th runat="server">
                                    Publish Date</th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="">
                        <asp:DataPager ID="DataPager1" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                </td>
                <td>
                    <asp:Label ID="IDLabel" runat="server" Text='<%# Eval("ID") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishYearLabel" runat="server" 
                        Text='<%# Eval("PublishYear") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishMonthLabel" runat="server" 
                        Text='<%# Eval("PublishMonth") %>' />
                </td>
                <td>
                    <asp:Label ID="PayDteLabel" runat="server" Text='<%# Eval("PayDte") %>' />
                </td>
                <td>
                    <asp:Label ID="PublishDteLabel" runat="server" 
                        Text='<%# Eval("PublishDte") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>

Upvotes: 0

Views: 1527

Answers (1)

ps2goat
ps2goat

Reputation: 8475

ListViews don't have columns, GridViews do. You must have some other control in your ListView that you want to hide. Please post your aspx code.

EDIT: (answer)

FindControl() only looks at the immediate children of the control, not the grandchildren, etc.

You will need to create a method that loops through all children to find what you want.

E.g.:

public Control MyFindControl(Control parent, string controlIdToFind)
{
    foreach(Control c in parent.Controls)
    {
        Control found = MyFindControl(c, controlIdToFind);
        if (found != null)
        {
           return found;
        }
    }

    // control not found.
    return null;
}

Then change your code to:

    ListView1.FindControl("idTh").Visible = false;
    foreach (ListViewItem item in ListView1.Items)
    {
        MyFindControl(item, "idCol").Visible = false;
    } 

Forgive me if my C# is rusty.

Upvotes: 1

Related Questions