Sabtu, 27 Juli 2013

ListView in windows forms C#

A ListView class displays a list of items with options of text, small and large images. If we look in our windows explorer of windows 8 and view menu in windows 7, there is a view tab in Quick Access Toolbar that have some listview’s options e.g. small icons, large icons, details, tiles etc. We can use the listview control to display data from our database or a text file.

The ListView class often used to display the collection of items in one of these five different views: Small icon, large icon, Tile, List and Details. Small icon and large icon view are simple to understand as items will be displayed as small icon, or large icon in the listview.  Tile view will display item and its subitems as a tile that contains a text information and a large icon. Details view will display item and its subitems in a grid with column headers that shows the information about that item. View List is well known to us as i.e. all the items are shown in a single list.

There are many properties of a listview control. In this post we will go through some most often used properties in C# programming.

  • AllowColumnReorder: user can reorder columns in details view using this property with dragging column headers.
  • CheckBoxes: checkboxes are displayed for each item to allow the user to check the items they want to work on.
  • Columns: get the list of column headers of listview. Only used in details view.
  • GridLines: enable or disable gridlines around the items and subitems. Only used in details view.
  • Scrollable: it shows the scroll bar if there are more items to be fit in client area.
  • TileSize: decide the size of the tile in Tile view.
  • TopItem: used to get or set the first visible item.
  • View: specify how items will be shown according to one of above five different views.

There are more properties of listview which can be easily used with their name in properties window of Visual Studio.

Let’s drag and drop a ListView control in windows form and add following items and finally view these items as large icons. This code is in C# language.
listView1.Items.Add("Item 1");
listView1.Items.Add("Item 2");
listView1.Items.Add("Item 3");
listView1.Items.Add("Item 4");
listView1.Items.Add("Item 5");
when we run this form then it will show the items as:

ListView large icon view

When we want to talk about to show the items in details view then we have to add some columns into this listview. If we will set the details view with above listview items then it will show nothing when we run this form. That’s why there should be some columns to use the details view in listview control. This code is in C# language.

listView1.Columns.Add("column1");
listView1.Columns.Add("column2");
listView1.Columns.Add("column3");

listView1.Items.Add(new ListViewItem(new[] { "value1", "value1", "value1" }));
listView1.Items.Add(new ListViewItem(new[] { "value2", "value2", "value2" }));
listView1.Items.Add(new ListViewItem(new[] { "value3", "value3", "value3" }));

Run this form now then it will show three columns as shown in below screenshot:

ListView details view

We can change some common options at design time as shown in below screenshot:
ListView design time editing

As the properties of listview class there are some events of listview class which occurred when something happens to listview. The most often used event is SelectedIndexChanged that occurs whenever the selected index property of the listview changed. The example of this event is:
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("SelectedIndexChanged event occur");
}
Now every time you change the selected index of listview this message will show in Message Box.

Tidak ada komentar:

Posting Komentar