Export (0) Print
Expand All
Expand Minimize

ListView.Clear Method

Removes all items and columns from the control.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public void Clear()

You can use this method to remove all items and columns from the ListView control without having to call the individual Clear methods from the ListView.ColumnHeaderCollection and ListView.ListViewItemCollection classes.

NoteNote

The Clear method does not clear Groups specified for the ListView control. To clear Groups call ListViewGroupCollection.Clear.

The following code example demonstrates the use of Clear and ListViewItem.Selected members. To run this example, paste the following code into a form that contains a ListView named ListView1 and a button, located toward the bottom of the form, named Button1. Call the InitializeListView method from the form's constructor or Load method.

	private void InitializeListView()
	{
		// Set up the inital values for the ListView and populate it. 
		this.ListView1 = new ListView();
		this.ListView1.Dock = DockStyle.Top;
		this.ListView1.Location = new System.Drawing.Point(0, 0);
		this.ListView1.Size = new System.Drawing.Size(292, 130);
		this.ListView1.View = View.Details;
		this.ListView1.FullRowSelect = true;

		string[] breakfast = new string[]{"Continental Breakfast", 
			"Pancakes and Sausage", "Denver Omelet", "Eggs & Bacon", 
			"Bagel & Cream Cheese"};

		string[] breakfastPrices = new string[]{"3.09", "4.09", 
			"4.19", "4.79", "2.09"};

		PopulateMenu("Breakfast", breakfast, breakfastPrices);
	}

	private void PopulateMenu(string meal, 
		string[] menuItems, string[] menuPrices)
	{
		ColumnHeader columnHeader1 = new ColumnHeader();
		columnHeader1.Text = meal + " Choices";
		columnHeader1.TextAlign = HorizontalAlignment.Left;
		columnHeader1.Width = 146;

		ColumnHeader columnHeader2 = new ColumnHeader();
		columnHeader2.Text = "Price";
		columnHeader2.TextAlign = HorizontalAlignment.Center;
		columnHeader2.Width = 142;

		this.ListView1.Columns.Add(columnHeader1);
		this.ListView1.Columns.Add(columnHeader2);

		for(int count=0; count < menuItems.Length; count++)
		{
			ListViewItem listItem = 
				new ListViewItem(menuItems[count]);
			listItem.SubItems.Add(menuPrices[count]);
			ListView1.Items.Add(listItem);
		}

		// Use the Selected property to select the first item on  
		// the list.
		ListView1.Focus();
		ListView1.Items[0].Selected = true;
	}


	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
		// Create new values for the ListView, clear the list,  
		// and repopulate it. 
		string[] lunch = new string[]{"Hamburger", "Grilled Cheese",
			"Soup & Salad", "Club Sandwich", "Hotdog"};

		string[] lunchPrices = new string[]{"4.09", "5.09", "5.19", 
			"4.79", "2.09"};

		ListView1.Clear();

		PopulateMenu("Lunch", lunch, lunchPrices);
		Button1.Enabled = false;
	}
and ListViewItem.Items.Selected members. To run the sample place the following code in a form
containing a ListView named ListView1 and a button, located toward the bottom of
the form, named Button1.  Call the InitializeListView method from the form's constructor

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft