views:

4488

answers:

8

Hi,

I try to clear my listview but the clear method doesn't work:

myListView.Items.Clear();

This doen't work. When i put a breakpoint at this line, the line is executed, but my listview isn't empty. How come??

I fill my listview by setting it's datasource to a datatable.

My solution now is to set the datasource to an empty datatable.

I just wonder why clear don't do the trick?

I use a master page. Here some code of a content page when a button is pressed. The method SearchTitle fills the ListView.

Relevant code:

        protected void Zoek()
    {
        // Clear listbox
        ListView1.DataSource = new DataTable();
        ListView1.DataBind();

        switch (ddlSearchType.SelectedValue)
        {
            case "Trefwoorden":
                SearchKeyword();
                break;
            case "Titel":
                SearchTitle();
                break;
            case "Inhoud":
                SearchContent();
                break;
        }
    }

Method that fills the ListView

        private void SearchTitle()
    {
        // Make panel visible
        pnlResult.Visible = true;
        pnlKeyword.Visible = false;

        Search Search = new Search(txtSearchFor.Text);
        ListView1.DataSource = Search.SearchTitle();
        ListView1.DataBind();
    }
+2  A: 

Try this ...

myListView.DataSource = null;
myListView.Items.Clear();
JaredPar
Tried that, doesn't work..
Martijn
A: 

My guess is that Clear() causes a Changed event to be sent, which in turn triggers an automatic update of your listview from the data source. So this is a feature, not a bug ;-)

Have you tried myListView.Clear() instead of myListView.Items.Clear()? Maybe that works better.

Treb
The clear method on myListView doesn't exist:Error 4 'System.Web.UI.WebControls.ListView' does not contain a definition for 'Clear' and no extension method 'Clear' accepting a first argument of type 'System.Web.UI.WebControls.ListView' could be found (are you missing a ....)
Martijn
That is because we were talking about different objects, I thought you were referring to a 'System.Windows.Forms.ListView', sorry. In that case you should try what others already recommended: Set the data source to null first.
Treb
+6  A: 

How about

DataSource = null;
DataBind();
Tony Basallo
does also work, but is similar to DataSource = new DataTable() (but yes, this is better i think)
Martijn
yeah creating an empty DataTable is a waste
Jason Miesionczek
A: 

I would suggest to remove the rows from the underlying DataTable, or if you don't need the datatable anymore, set the datasource to null.

Martin Moser
A: 

Probably your code works but it is rebound somewhere after you clear it. Make sure that this it not the case. It will be more helpful if you provide some code. Where are you setting your data source? Where are you data binding? Where are you clearing the list?

Serhat Özgel
I have added code to my startpost.
Martijn
I saw that but could not see a problem with your code.
Serhat Özgel
A: 

I have implemented Listview with Updatepanel (VS 2008) and Using LNQ to SQL. Data Binding very cool. I ahve other section these are lots fields to make mannually Filter and Sorting option to for this ListView. When I making new dynamic query to make filter that filter query bringing exact filter row but listView not even refflecting or binding again new data and Old data is displaying as it is. I have used {lvInventoryList.Items.Clear();} seems not working. Any Quick idea will be higly appriciated.

This code I'm using on filter button or (option) -_________________________________ lvInventoryList.DataSource = null; lvInventoryList.DataBind(); lvInventoryList.Items.Clear(); lvInventoryList.Controls.Clear();

            ViewInventoryListBAL objA = new ViewInventoryListBAL();
            lvInventoryList.DataSource = objA.GetInventoryFilterList(Convert.ToInt32(ddlViewRecords.SelectedItem.Text), Where);
            lvInventoryList.DataBind();


Below is my ListView Desg. Code

<%----%>    Actions Groups Exotic Regular# Exotic# Online# Virtual# Year Make Model Body Cost Check# Market Price Mileage VIN# Title Dealer Desgn. Notes ' class="GridContent"> '>

                                                                <asp:Label ID="lblIsExotic" runat="server" Text='<%# Eval("IsExotic").ToString().ToLower() != "true" ? "No" : "Yes" %>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblRegularLane" runat="server" Text='<%# Eval("RegularLane") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td id="coExoticRow_<%# Container.DataItemIndex %>" class="GridContent" style="width: 100px;
                                                                display: none"><asp:Label ID="lblExoticLane" runat="server" Text='<%# Eval("ExoticLane") ?? "&nbsp;"%> '></asp:Label>
                                                            </td>
                                                            <td id="coOnlineRow_<%# Container.DataItemIndex %>" class="GridContent" style="width: 100px;
                                                                display: none">

                                                                <asp:Label ID="lblOnlineLane" runat="server" Text=' <%# Eval("OnlineLane") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td id="coVirtualRow_<%# Container.DataItemIndex %>" class="GridContent" style="width: 100px;
                                                                display: none">
                                                                <%# Eval("VirtualLane") ?? "&nbsp;"%>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblYear" runat="server" Text='<%# Eval("Year") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblMake" runat="server" Text='<%# Eval("Make") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblModel" runat="server" Text='<%# Eval("Model") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblBody" runat="server" Text='<%# Eval("Body") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lnlCarCost" runat="server" Text='<%# Eval("CarCost") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblCheckNumber" runat="server" Text=' <%# Eval("CheckNumber") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblMarketPrice" runat="server" Text='<%# Eval("MarketPrice") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblMileageIn" runat="server" Text='<%# Eval("MileageIn") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">
                                                                <asp:LinkButton ID="lnkVIN" runat="server"><%# Eval("VIN") ?? "&nbsp;"%></asp:LinkButton>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="lblDealerName" runat="server" Text='<%# Eval("DealerName") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 100px">

                                                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Designation") ?? "&nbsp;"%>'></asp:Label>
                                                            </td>
                                                            <td class="GridContent" style="width: 50px">
                                                                <asp:LinkButton ID="lnkNotes" runat="server"> <%# Eval("Notes") ?? "&nbsp;"%></asp:LinkButton>
                                                            </td>
                                                    </tr>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <%--Edit Part Start Here--%>
                                                    <tr class='edit-info <%# Container.DataItemIndex == 0 ? "first" : string.Empty %>'>
                                                        <td class="GridContent" style="width: 100px">
                                                            <table class="TableHeading">
                                                                <tr>
                                                                    <td>
                                                                        <asp:ImageButton ID="btnEdit2" runat="server" ImageUrl="~/Images/edit-icon.jpg" CommandName="Edit"
                                                                            AlternateText="Edit" />
                                                                    </td>
                                                                    <td>
                                                                        <asp:ImageButton ID="btnHistory2" runat="server" ImageUrl="~/Images/FreshRecords.jpg"
                                                                            CommandName="History" AlternateText="History" />
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("GRP")%>
                                                        </td>
                                                        <td class="GridContent" style="width: 50px">
                                                            <%# Eval("IsExotic").ToString().ToLower() != "true" ? "No" : "Yes" %>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("RegularLane") ?? "&nbsp;"%>
                                                        </td>
                                                        <td id="coExoticEdit" class="GridContent" style="width: 100px; display: none">
                                                            <%# Eval("ExoticLane") ?? "&nbsp;"%>
                                                        </td>
                                                        <td id="coOnlineEdit" class="GridContent" style="width: 100px; display: none">
                                                            <%# Eval("OnlineLane") ?? "&nbsp;"%>
                                                        </td>
                                                        <td id="coVirtualEdit" class="GridContent" style="width: 100px; display: none">
                                                            <%# Eval("VirtualLane") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Year") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Make") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Model") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Body") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("CarCost") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("CheckNumber") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("MarketPrice") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("MileageIn") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("VIN") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Title") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("DealerName") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 100px">
                                                            <%# Eval("Designation") ?? "&nbsp;"%>
                                                        </td>
                                                        <td class="GridContent" style="width: 50px">
                                                            <%# Eval("Notes") ?? "&nbsp;"%>
                                                        </td>
                                                    </tr>
                                                    <tr style="width: 50%">
                                                        <td class="GridContent" colspan="21">
                                                            <%--<div >Edit details for '<%# Eval("InventoryId")%>'</div>--%>
                                                            <table class="ForLegend" cellpadding="0" cellspacing="0" border="0">
                                                                <tr>
                                                                    <%--Edit First Line Header Part--%>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkExotic" CssClass="ListViewEditContent" Text="Exotic" runat="server"
                                                                            Checked='<%# Bind("IsExotic") %>' AutoPostBack="true" TextAlign="Left" />
                                                                    </td>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        <asp:Label ID="lblGp1" runat="server"></asp:Label>
                                                                    </th>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkG1" runat="server" />
                                                                    </td>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        <asp:Label ID="lblGp2" Width="75px" runat="server"></asp:Label>&nbsp;
                                                                    </th>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkG2" runat="server" />
                                                                    </td>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        <asp:Label ID="lblGp3" Width="75px" runat="server"></asp:Label>
                                                                    </th>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkG3" runat="server" />
                                                                    </td>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        <asp:Label ID="lblGp4" Width="75px" runat="server"></asp:Label>
                                                                    </th>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkG4" Width="75px" runat="server" />
                                                                    </td>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        <asp:Label ID="lblGp5" Width="75px" runat="server"></asp:Label>
                                                                    </th>
                                                                    <td class="ListViewEditContent">
                                                                        <asp:CheckBox ID="chkG5" runat="server" />
                                                                    </td>
                                                                    <th>
                                                                        <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="~/Images/Cancel.gif" CommandName="Cancel"
                                                                            AlternateText="Cancel" />
                                                                    </th>
                                                                </tr>
                                                                <tr>
                                                                    <th class="ListViewEditContent" align="right">
                                                                        Regular#&nbsp;
                                                                    </th>
A: 

Don't bother with Clear(). Just do this: ListView.DataSource = null; ListView.DataBind();

The key is the databind(); Works everytime for me.

Dave Howell
A: 

The Problem is arising because you are trying to clear the entire list box. Just use listView1.Items.Clear();

and everything will work like a charm regds saurabh gupta

saurabh gupta