Tuesday, July 28, 2009

Windows Forms ListView

ListView is a very important control in Windows Forms. ListView can be used to display complex data list employee information, file information and the like.
You can see a list view in action in the Windows Explorer's right hand side.
Listview comes with various Views like SmallIcon, LargeIcon, Tile, List and Detail.

In this blog I am going to concentrate on DetailsView.

private void Form1_Load(object sender, EventArgs e)
listView1.View = View.Details;

//Adding Columns
listView1.Columns.Add("Blogs", 170);
listView1.Columns.Add("Blogger" , 100);

//Adding a ListView Item
ListViewItem item1 = new ListViewItem("shalvin.com", 0);
item1.SubItems.Add("Shalvin P D");
ListViewItem item2 = new ListViewItem("shalvinpd.blogspot.com", 0);
item2.SubItems.Add("Shalvin P D");

First I am adding Header to ListBox using Columns.Add method passing in the HeaderName and width.
Then I am adding items to ListView using ListViewItem Class which represents an item in ListView and then adding the SubItem to ListView using Items.Add.

It is possible to change the look of in item in ListView using the ListViewItem class :

Font f = new Font(FontFamily.GenericSerif, 10, FontStyle.Bold ^ FontStyle.Italic);
item1.BackColor = Color.LightBlue;
item1.Font = f;

Working with ListView Visually

No comments:

Post a Comment