Thursday, November 22, 2012

Binding data to a ListView with Multiple Columns in C# Windows Form

A ListBox control in a C# windows forms, can display only one column of data, but there are instances when we want to display multiple columns of data. One option is to use the DataGrid control, but we can also use the ListView control to display multiple column data.

The
ListView can be used to display static information, which the user will not be changing, in this post we shall see on how to use the ListView control to display multiple columns of data in a C# Windows Form. We shall bind the data from the list view from the code behind file.


1. Create a new / open an existing C# Windows application
2. Open and existing / Add a new Windows Form to the Project
3. Drag
a ListView control into the form.
4. Open the Properties window of the ListView.
5. Open the Columns (Collection) Property of the
ListView and add the Columns Required.
6. Now switch to the code behind and add the following code to populate the ListView.

            ListViewItem listViewItem;
            //
            listViewItem = new ListViewItem("<= 18.5");
            listViewItem.SubItems.Add("Underweight");
            listView1.Items.Add(listViewItem);
            //
            listViewItem = new ListViewItem("18.5 to 25");
            listViewItem.SubItems.Add("Normal Weight");
            listView1.Items.Add(listViewItem);
            //
            listViewItem = new ListViewItem("25.1 to 30");
            listViewItem.SubItems.Add("Overweight");
            listView1.Items.Add(listViewItem);
            //
            listViewItem = new ListViewItem("> 30");
            listViewItem.SubItems.Add("Obese");
            listView1.Items.Add(listViewItem);

7. The Final ListView looks as follows.



Search Flipkart Products:
Flipkart.com

1 comment:

Unknown said...

Hi!

This is a nice article. Thank for sharing your knowledge. There are some other links related to "Binding XML Data to

ListView Control in C#". I hope this is a very useful for developers.

http://www.mindstick.com/Articles/b065d7e4-e6a5-4d01-b66f-de1400e63916/?Binding%20XML%20Data%20to%20ListView%20Control

http://www.c-sharpcorner.com/UploadFile/c5c6e2/binding-xml-data-to-listview-control-dataset-approach/